#include<iostream>
using namespace std;
char s[55][55];
int visited[55][55];
int vis[55][55];
char shang[8] = "+|TS";
char xia[8]= "+|TS.";
char zuo[8] = "+-TS";
char you[8]= "+-TS";
bool f(char cc[8], char e) {
if (e == 0)return false;
for (int i = 0; i < 8; ++i) {
if (cc[i] == e) return true;
}
return false;
}
void dfs(int r, int c) {
switch (s[r][c])
{
case 0:break;
case '#':break;
case '+':
case 'S':
case 'T':
if (!visited[r - 1][c]) { visited[r - 1][c] = 1; dfs(r - 1, c); }
if (!visited[r + 1][c]) { visited[r + 1][c] = 1; dfs(r + 1, c); }
if (!visited[r][c - 1]) { visited[r][c - 1] = 1; dfs(r, c - 1); }
if (!visited[r][c + 1]) { visited[r][c + 1] = 1; dfs(r, c + 1); }
break;
case '-':
if (!visited[r][c - 1]) { visited[r][c - 1] = 1; dfs(r, c - 1); }
if (!visited[r][c + 1]) { visited[r][c + 1] = 1; dfs(r, c + 1); }
break;
case '|':
if (!visited[r - 1][c]) { visited[r - 1][c] = 1; dfs(r - 1, c); }
if (!visited[r + 1][c]) { visited[r + 1][c] = 1; dfs(r + 1, c); }
break;
case '.':
if (!visited[r + 1][c]) {visited[r + 1][c] = 1; dfs(r + 1, c); }
default:
break;
}
}
void dfs2(int r, int c) {
if (!vis[r - 1][c] && f(xia, s[r - 1][c])) { vis[r - 1][c] = 1; dfs2(r - 1, c); }
if (!vis[r + 1][c] && f(shang, s[r + 1][c])) { vis[r + 1][c] = 1; dfs2(r + 1, c); }
if (!vis[r][c-1] && f(you, s[r][c-1])) { vis[r][c-1] = 1; dfs2(r, c-1); }
if (!vis[r][c + 1] && f(zuo, s[r][c + 1])) { vis[r][c + 1] = 1; dfs2(r, c +1); }
}
int main()
{
int R, C;
cin >> R >> C;
int sr, sc, tr, tc; getchar();
for (int i = 1; i <= R; ++i) {
for (int j = 1; j <= C; ++j) {
cin >> s[i][j];
if (s[i][j] == 'S') { sr = i; sc = j; }
if (s[i][j] == 'T') { tr = i; tc = j; }
}
getchar();
}
visited[sr][sc] = 1;
dfs(sr,sc);
if (!visited[tr][tc]) {
cout << "I'm stuck!" << endl; return 0;
}
vis[tr][tc] = 1;
dfs2(tr, tc);
int cnt = 0;
for (int i = 1; i <= R; ++i)
for (int j = 1; j <= C; ++j)
if(!vis[i][j] && visited[i][j] && s[i][j]!='#')
++cnt;
cout << cnt << endl;
}
ccf 201312-5 I’m stuck!
最新推荐文章于 2021-08-24 16:12:49 发布