ZOJ Problem Set

来源:互联网 发布:js的单选按钮的事件 编辑:程序博客网 时间:2024/06/05 09:56

ZOJ Problem Set - 2100

题意:一块田地,播种机从左上出发,不能重复走,不能走石头。判断能不能。
思路:简单的dfs。
细节:走过的标记成石头,不行就改回来。

#include <iostream>#include <cstdio>#include <cstring>using namespace std;int n, m;char a[10][10];int num , flag;void dfs(int x, int y){    if(x < 1 || x > n || y < 1|| y > m) return;    if(a[x][y] == 'S') return;    num++; a[x][y] = 'S';    if(num == n*m){        flag = 1;return;    }    dfs(x+1, y);    dfs(x-1, y);    dfs(x, y+1);    dfs(x, y-1);    num--; a[x][y] = '.';}int main(){     while(cin >> n >> m && n && m){        num = 0; flag = 0;        for(int i = 1; i <= n; i++)        for(int j = 1; j <= m; j++){            cin >> a[i][j];            if(a[i][j] == 'S') num++;        }        dfs(1,1);        if(flag) cout << "YES" << endl;            else cout << "NO" << endl;     }}