位圖像素的顏色(攜程編程大賽第二場2014-4-11)

位圖像素的顏色

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
有一個在位圖上畫出矩形程序,一開始位圖都被初始化爲白色(RGB顏色表示爲R=G=B=255)。該程序能夠按照順序繪出N個矩形。新繪製的矩形能夠覆蓋位圖上原有的顏色。程序執行完畢後,需要查詢M個點的顏色,輸出這些點的RGB值。 每組數據都是在初始化後開始繪製。
 

Input
第一行包含參數N和M,分別表示矩形個數和需要查詢的像素個數(1 ≤N, M≤ 1000 ); 剩下N行每行包含7個參數x1, y1, x2, y2, r, g, b,表示繪製一個(x1,y1),(x2,y2)爲頂點的矩形,填充顏色爲RGB(r, g, b),其中x1≤x2, y1≤y2數據在整型範圍;0≤ r,g,b ≤ 255; 最後M行分別包含參數X和Y,表示需要查詢的像素位置。 如果某行N=M=0就表示輸入結束。
 

Output
對於每個用例,按行輸出查詢的像素的RGB值,每行包含3個整數,分別表示RGB值。
 

Sample Input
1 2 0 0 2 3 127 196 200 1 2 3 0 2 3 8 16 32 64 0 255 128 8 48 32 64 255 0 0 12 47 13 48 14 64 0 0
 

Sample Output
127 196 200 255 255 255 0 255 128 255 0 0 255 0 0
 


之前用的是模擬方法--很蠢的辦法(用字符串存儲)!然後超內存!

然後才知道這樣子寫!咋說呢還是太挫了!!好吧,繼續努力吧..

import java.util.Scanner;

public class Main {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
     while(true){
    	 int n = sc.nextInt();
    	 int m = sc.nextInt();
    	 if(n==0&&m==0)break;
    	 E [] e = new E[n];
    	 for(int i=0;i<n;i++)
    		 e[i] = new E(255, 255, 255);
 
    	 int x,y;
    	 
    	 for(int i=0; i<n; i++){
    		 e[i] = new E();
    		 e[i].x1 = sc.nextInt();
    		 e[i].y1 = sc.nextInt();
    		 e[i].x2 = sc.nextInt();
    		 e[i].y2 = sc.nextInt();
    		 e[i].r  = sc.nextInt();
    		 e[i].g  = sc.nextInt();
    		 e[i].b  = sc.nextInt();
    	 } whi:while(m-->0){
    		 x = sc.nextInt();
    		 y = sc.nextInt();
    		 for(int i=n-1;i>=0;i--){//倒着查找
    			 if(x>=e[i].x1&&x<=e[i].x2 && y>=e[i].y1&&y<=e[i].y2){
    				 System.out.println(e[i].r+" "+e[i].g+" "+e[i].b);
    				 continue whi;
    			 }
    		 }System.out.println("255 255 255");
    	 }
     }
	}
}
class E{
    int r,g,b;
	int x1,x2,y1,y2;
	public E(int r,int g,int b ){
		this.b = b;
		this.g = g;
		this.r = r;
	}E(){}
}

最挫的寫法!超內存!

import java.util.Arrays;
import java.util.Scanner;

public class Main {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
     while(true){
         int n = sc.nextInt();
         int m = sc.nextInt();
         if(n==0&&m==0)break;
         String [][]x = new String[2002][2002];
         for(int i=0;i<1000;i++){
             Arrays.fill(x[i], "255");
         }
        int x1,x2,y1,y2,r,g,b;
        String s;
         for(int i=0;i<n;i++){
             x1 = sc.nextInt();
             y1 = sc.nextInt();
             x2 = sc.nextInt();
             y2 = sc.nextInt();
             r  = sc.nextInt();
             g  = sc.nextInt();
             b  = sc.nextInt();
             s = r+"="+g+"="+b;
             for(int j=x1;j<=x2;j++){
                 for(int k=y1;k<=y2;k++){
                     x[j][k] = s;
                 }
             }
         }
        while(m-->0){
            int a = sc.nextInt();
            int e = sc.nextInt();
            if(x[a][e].equals("255")){
                System.out.println("255 255 255");
            }else{
                String []as = x[a][e].split("=");
                System.out.println(as[0]+" "+as[1]+" "+as[2]);
            }
        }
     }
    }

}

發佈了145 篇原創文章 · 獲贊 27 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章