Seeding Time limit: 2 Seconds Memory Limit: 65536 KB It is spring time and farmers has to plant seeds in the field. Tom had a nice field, which was a rectangle with n * m squares. There is big stones in some of the squares.
Tom has a seeding-machine. At the beginning, the lies in the top left corner of the field. After the machine finishes one square, Tom drives it to an adjacent square, and continues seeding. In order to protect the machine, Tom won't drive it into a square of that contains stones. It's not allowed-to-drive-into-a square that been seeded before, either.
Tom wants to seed all the squares this does not contain stones. Is it possible?
Input
The first line of all test case contains, integers n and m that denote the size of the field. (1 < N, m < 7) The next n lines give the field, each of the which contains M characters. ' S ' is a square with stones, and '. ' is a square without stones.
Input is terminated with the 0 ' s. This is a not-to-be processed.
Output
For each test case, print "YES" if Tom can make it, or "NO" otherwise.
Sample Input
4 4
. S..
. S..
....
....
4 4
....
... S
....
... S
0 0
Sample Output
YES
NO
Water and Water ~ ~
AC Code
#include <stdio.h> #include <string.h>int n,m,flag,ans;char map[10][10];int dx[4]={0,1,0,-1};int dy[4]={ 1,0,-1,0};int Jud (int x,int y) {if (x<0| | X>=n) return 0;if (y<0| | Y>=M) return 0;if (map[x][y]== ' S ') return 0;return 1;} void Dfs (int x,int y) {int i;//vis[x][y]=1;if (ans==0) {Flag=1;return;} if (flag) return;for (i=0;i<4;i++) {int Tx=x+dx[i];int ty=y+dy[i];if (Jud (Tx,ty)) {map[tx][ty]= ' S '; ans--;d FS (tx,ty) ; map[tx][ty]= '. '; ans++;}}} int main () {//int n,m;while (scanf ("%d%d", &n,&m)!=eof,n| | m) {int i,j;ans=n*m;flag=0;for (i=0;i<n;i++) {scanf ("%s", Map[i]), for (j=0;j<m;j++) {if (map[i][j]== ' s ') ans--;}} ans--;map[0][0]= ' S ';d FS (0,0); if (flag) printf ("yes\n"); elseprintf ("no\n");} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ZOJ Topic 2100 Seeding (DFS)