ZOJ 2100 Seeding ( DFS 经典回溯
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 Input4 4 .S.. .S.. .... .... 4 4 .... ...S .... ...S 0 0Sample OutputYES NO題意
一個(gè)N*M的矩陣 里面有“S”和“.”,從左上方0 0點(diǎn)開始能否將.走一邊
直接回溯就行了, 非常裸的一道題,就是考察個(gè)回溯
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_JUDGEfreopen("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";elsecout << "NO\n";}return 0; }總結(jié)
以上是生活随笔為你收集整理的ZOJ 2100 Seeding ( DFS 经典回溯的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: poi导出word 表格 单元格内换行
- 下一篇: 软件测试线下培训比线上好在哪里?