[2013 Final] Colors
Description
Ziyao has a big drawing board with N * M squares. At the beginning, all the squares on the board are white, represented by the number 0. We can see the 4 * 4 whilte drawing board is:
0000
0000
0000
0000
One day Ziyao wants to draw on the board. He will draw for T times. Each time he will draw a rectangle with the color he like.For each rectangle, we use 4 integers (x1, y1, x2, y2) to express it. It means all squares whose coordinate (x, y) exist both x1 <= x <=x2 and y1 <= y <= y2. If two rectangles are intersected, the intersected part will be covered by the color later drawing.
For example, if he draw a rectangle (1, 2, 3, 3) on the white board with the color ‘1’, the board will be:
0110
0110
0110
0000
And if he go on drawing a rectangle (2, 1, 3, 2) by ‘2’, the board will be:
0110
2210
2210
0000
Now, Ziyao Big God will tell you his drawing process, please tell me how many colors will be there on the board.
Input
The first line has 3 integers, N, M, T.(0 < N, M <= 100, 0 <= T <=20)
The next T lines, each line has 5 integers x1, y1, x2, y2, c, (x1, y1, x2, y2) express the rectangle and c is the color.(1 <= x1, x2 <= N, 1 <= y1, y2 <=M, 1 <= c <= T)
Output
Just one number, how many colors on the board.
PS:’0’ is also a kind of color.
Sample Input 1
4 4 2
1 2 2 3 1
2 1 3 2 2
Sample Input 2
4 4 2
1 1 1 1 2
1 1 4 4 1
Sample Output 1
3
Sample Output 2
1
Hint
For the first sample, the board is
0110
2210
2200
0000
There are 3 colors:0,1,2.
For the second sample, the board is
1111
1111
1111
1111
There are only 1 color: 1.
这道题的话还好,主要注意的是题目的第一行或者列是数组下标为0的时候,这里要注意,还有就是怎么算出总共的颜色有多少种,我是另外开个数组,然后有数字的就做上标记
我的
#include<stdio.h> int a[100][100] = {0}; int main() { int n, m, t; int b[200] = {0}; int x1, y1, x2, y2, c, sum = 0, flag = 0; scanf("%d %d %d", &n, &m, &t); while (t--) { scanf("%d %d %d %d %d", &x1, &y1,&x2, &y2, &c); x1 -= 1; x2 -= 1; y1 -= 1; y2 -= 1; for (int i = x1; i <= x2; i++) { for (int j = y1; j <= y2; j++) { a[i][j] = c; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int temp = a[i][j]; b[temp]++; } } for (int i = 0; i < 200; i++) { if (b[i] != 0) sum++; } printf("%d", sum); return 0; }
标程(思想差不多)
1.#include <stdio.h> 2.int n, m, t, a[100][100], i, j, ans; 3.int main() { 4. int x1, y1, x2, y2, c, colors[21]; 5. scanf("%d%d%d", &n, &m, &t); 6. for (i = 0; i < n; i++) 7. for (j = 0; j < n; j++) a[i][j] = 0; 8. while (t--) { 9. scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &c); 10. for (i = x1; i <= x2; i++) 11. for (j = y1; j <= y2; j++) 12. a[i - 1][j - 1] = c; 13. } 14. for (i = 0; i <= 20; i++) colors[i] = 0; 15. for (i = 0; i < n; i++) 16. for (j = 0; j < m; j++) 17. colors[a[i][j]] = 1;\\原来是可以这样的 18. ans = 0; 19. for (i = 0; i <= 20; i++) ans += colors[i]; 20. printf("%d", ans); 21. return 0; 22.}