Seeding
题目描述
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?
输入
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.
输出
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
题意
一个N*M的矩阵 里面有“S”和“.”,从左上方0 0点开始能否将.走一边
直接回溯就行了, 非常裸的一道题,就是考察个回溯
AC代码
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
#define ls st<<1
#define rs st<<1|1
#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define LL long long
#define PII pair<int,int>
#define VI vector<int>
#define CLR(a,b) memset(a, (b), sizeof(a))
#define ALL(x) x.begin(),x.end()
#define rep(i,s,e) for(int i=(s); i<=(e); i++)
#define tep(i,s,e) for(int i=(s); i>=(e); i--)
const int INF = 0x3f3f3f3f;
const int MAXN = 2e2+10;
const int mod = 1e9+7;
const double eps = 1e-8;
void fe() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt","w",stdout);
#endif
}
LL read()
{
LL x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
int n, m;
int k, kk;
bool flag;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
char mps[MAXN][MAXN];
bool vis[MAXN][MAXN];
bool check(int x, int y) {
return x>=0&&x<n&&y>=0&&y<m&&mps[x][y]!='S'&&(!vis[x][y]);
}
void dfs(int x, int y) {
if(kk == n*m-k) {
flag = true;
return ;
}
for(int i = 0; i < 4; i++) {
int xx = dx[i]+x;
int yy = dy[i]+y;
if(check(xx, yy)) {
vis[xx][yy] = true;
kk++;
dfs(xx, yy);
kk--;
vis[xx][yy] = false;
}
}
}
int main(int argc, char const *argv[])
{
while(cin >> n >> m,n|m) {
CLR(vis, false);
CLR(mps, false);
flag = false;
k = 0, kk = 1;
for(int i = 0; i < n; i++)
cin >> mps[i];
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(mps[i][j] == 'S')
k++;
}
}
vis[0][0] = true;
dfs(0,0);
if(flag)
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}