zoj 2100 DFS
2017-07-20 20:52
155 查看
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 4
.S..
.S..
….
….
4 4
….
…S
….
…S
0 0
Sample Output
YES
NO
题意:裸的dfs
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 4
.S..
.S..
….
….
4 4
….
…S
….
…S
0 0
Sample Output
YES
NO
题意:裸的dfs
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; int n,m; char mp[10][10]; int tot; int dis[4][2]={1,0,0,1,-1,0,0,-1}; int flag; int check(int x,int y) { if(x<0||x>=n||y<0||y>=m) return 1; if(mp[x][y]=='S') return 1; return 0; } void dfs(int step,int x,int y) { // cout<<flag<<endl; if(flag==1) return ; // cout<<"##"<<endl; if(step==tot) { //cout<<tot<<endl; flag=1; return ; } for(int i=0;i<4;i++) { int curx=x+dis[i][0]; int cury=y+dis[i][1]; // cout<<curx<<","<<cury<<endl; if(check(curx,cury)) continue; mp[curx][cury]='S'; dfs(step+1,curx,cury); mp[curx][cury]='.'; } } int main() { // freopen("input.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; tot=0; for(int i=0;i<n;i++) { scanf("%s",mp[i]); } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { // cout<<mp[i][j]; if(mp[i][j]=='.') tot++; } // cout<<endl; } mp[0][0]='S'; flag=0; dfs(1,0,0); if(flag==1) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- zoj2100(DFS)
- ZOJ Problem Set - 2100 (DFS)
- zoj 2100 dfs
- zoj 2100 Seeding(DFS水水~)
- (DFS)ZOJ 2100 Seeding
- ZOJ2100题解这是我整整意义上的第一道DFS题能做出来我很开心,和上题一样,方法思路大体相同,不过不需要剪枝。
- zoj 2100 seeding(dfs)
- !ZOJ 2100--DFS(递归结束条件和回溯还是没理解好)
- ZOJ 2100 seeding 【DFS】
- zoj 2100 Seeding 【DFS 判断是否能经过每个点一次遍历完整个图】
- ZOJ 2100 Seeding(简单DFS)
- zoj 2100 Seeding(dfs)
- ZOJ 题目2100 Seeding(DFS)
- zoj 2100 dfs暴力宽搜
- zoj2100 Seeding (DFS递归+回溯)
- zoj 2100 水dfs
- ZOJ 2100--Seeding【DFS】
- zoj 2100(dfs深度优先遍历)
- zoj 3158 DFS
- zoj 2100 seeding