Zoj problem set-2100 Seeding
Time limit:1 second Memory limit:32768 KB
It is spring time and farmers have to plant seeds in the field. Tom has a nice field, which is a rectangle with N * m squares. There are big stones in some of the squares.
Tom has a seeding-machine. at the beginning, the machine lies in the top left corner of the field. after the machine finishes one square, Tom drives it into an adjacent square, and continues seeding. in order to protect the machine, Tom will not drive it into a square that contains stones. it is not allowed to drive the machine into a square that been seeded before, either.
Tom wants to seed all the squares that do not contain stones. Is it possible?
Input
The first line of each test case contains two integers n and M that denote the size of the field. (1 <n, m <7) The next n lines give the field, each of which contains M characters. 'S 'is a square with stones, and '. is a square without stones.
Input is terminated with two 0's. This case is not to be processed.
Output
For each test case, print "yes" if Tom can make it, or "no" otherwise.
Sample Input
4
. S ..
. S ..
....
....
4
....
... S
....
... S
0 0
Sample output
Yes
No
==================
Typical DFS questions, very good.
the question is very simple. Start from the upper left corner to determine whether the ground can be completely flushed ( the tractor cannot drive on rocks or ground that has already been flushed. )
#include
# include
int dir [4] [2] = {}, {}, {-}, {0,-1 }}; // four direction char map [10] [10]; // defines a map int done; int flag; int n, m; void DFS (int x, int y) {int I; If (x <1 | Y <1 | x> N | Y> m) return; // determine whether the done ++ is out of bounds; if (done = N * m) // if the request succeeds, {flag = 1; return ;}for (I = 0; I <4; I ++) is returned) {If (Map [x + dir [I] [0] [Y + dir [I] [1] = '. ') {map [x + dir [I] [0] [Y + dir [I] [1] = 's'; // mark DFS (x + dir [I] [0], Y + dir [I] [1]);} done --; map [x] [Y] = '. ';} int main () {int I, j; memset (MAP,'s', sizeof (MAP )); while (scanf ("% d % * C", & N, & M )! = EOF) {If (n = 0 & M = 0) break; done = 0; flag = 0; for (I = 1; I <= N; I ++) {for (j = 1; j <= m; j ++) {scanf ("% C", & map [I] [J]); if (Map [I] [J] = 's') done ++;} getchar ();} map [1] [1] ='s '; DFS (1, 1); If (flag = 1) printf ("Yes \ n"); else printf ("NO \ n") ;}
return 0 ;}