列表

详情


NC211820. WalkingMachine

描述

Given a maze of size , where upper left corner is and lower right corner is . For each cell , there is exactly one character on it, denoting the moving restriction:

* If , you can only go up from this cell, specifically go from to

* If , you can only go left from this cell, specifically go from to

* If , you can only go down from this cell, specifically go from to

* If , you can only go right from this cell, specifically go from to

We say one is out if or or or holds, now you should determine the number of cells that one can be out if start walking on it.

输入描述

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 (1, 4), (2, 1), (3, 1), (3, 2), (3, 3), (3, 4)_{} 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);
}

上一题