The idea is greedy, and the method is very watery.
Practice:
1. Each time a robot is dispatched to the right or down from () until (24, 24.
2. If the current grid has garbage, clear it.
3. If there is garbage on the right of the current grid, go to the right. Otherwise, go down.
4. If you go to the bottom, go to the right. If you go to the far right, go down.
5. After a robot completes, check whether there is garbage. If there is one, send another one. If not, stop.
Right [I] [J] is the number of spams on the right of (I, j;
Down [I] [J] is the number of spams under (I, j;
Total number of spams recorded by CNT;
Res records the number of robots.
Source code
| Problem: 1548 |
|
User: yueashuxia |
| Memory: 400 K |
|
Time: 0 ms |
| Language: gcc |
|
Result: accepted |
#include<stdio.h>char grid[25][25];int Right[25][25],Down[25][25];int main(){ int a, b, i, j, k, cnt = 0, res; while(scanf("%d%d", &a, &b), a != -1 || b != -1) { grid[a][b] = 1; cnt ++; for(i = 1; i < b; i ++) { Right[a][i]++; } for(i = 1; i < a; i ++) { Down[i][b]++; } if(a == 0 && b == 0) { cnt--; for(res = 0; cnt > 0; ) { res++; i = j = 1; while(i < 25 && j < 25) { if(grid[i][j] == 1) { grid[i][j] = 0; cnt--; for(k = 1; k < i; k ++) Down[k][j]--; for(k = 1; k < j; k ++) Right[i][k]--; } if(cnt == 0) break; if(Right[i][j] > 0) { if(j+1 > 24) i++; else j++; } else { if(i+1 > 24) j++; else i++; } } } printf("%d\n", res); } } return 0;}