NC211820. WalkingMachine
描述
输入描述
The first line contains two integers , denoting the size of the maze.
Following lines each contains a string of length , where the -th character in -th line denotes the character on cell .
输出描述
Only one line containing one integer, denoting the number of cells that can make one out.
示例1
输入:
3 4 DDSD AWAA WASD
输出:
6
说明:
The 6 cells are respectively.C++(clang++11) 解法, 执行用时: 25ms, 内存消耗: 5308K, 提交时间: 2020-10-25 12:47:59
#include<cstdio> int n,m,ans,vis[1005][1005];char s[1005][1005]; int work(int x,int y) { if(x<1||x>n||y<1||y>m)return 1; if(vis[x][y])return vis[x][y]; vis[x][y]=2;int xx=x,yy=y; if(s[x][y]=='W')x--;else if(s[x][y]=='S')x++;else if(s[x][y]=='A')y--;else if(s[x][y]=='D')y++; return vis[xx][yy]=work(x,y); } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%s",s[i]+1); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(!vis[i][j])work(i,j); if(vis[i][j]==1)ans++; } printf("%d\n",ans); }