日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ZOJ 2100 Seeding ( DFS 经典回溯

發布時間:2024/5/15 编程问答 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

題意

一個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_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; }

總結

以上是生活随笔為你收集整理的ZOJ 2100 Seeding ( DFS 经典回溯的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。