Good Bye 2017-B. New Year and Buggy Bot

B. New Year and Buggy Bot
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Bob programmed a robot to navigate through a 2d maze.

The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'.

There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character 'E'. This position has no obstacle in it.

The robot can only move up, left, right, or down.

When Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits.

The robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions.

Bob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit.

Input

The first line of input will contain two integers n and m (2 ≤ n, m ≤ 50), denoting the dimensions of the maze.

The next n lines will contain exactly m characters each, denoting the maze.

Each character of the maze will be '.', '#', 'S', or 'E'.

There will be exactly one 'S' and exactly one 'E' in the maze.

The last line will contain a single string s (1 ≤ |s| ≤ 100) — the instructions given to the robot. Each character of s is a digit from 0 to 3.

Output

Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit.

Examples
input
5 6
.....#
S....#
.#....
.#....
...E..
333300012
output
1
input
6 6
......
......
..SE..
......
......
......
01232123212302123021
output
14
input
5 3
...
.S.
###
.E.
...
3
output
0
Note

For the first sample, the only valid mapping is , where D is down, L is left, U is up, R is right.

考试的时候这题居然Wa了,后来仔细一看,发现m打成n了,吐血……

Code:

#include<bits/stdc++.h>
using namespace std;
char s[55][55],st[105];
int a[5],b[5],c[5],f[5][5],x1,x2,y11,y2,n,m,ans;
void check()
{
	int x=x1,y=y11;bool flag=false;
	for(int i=0;i<strlen(st);i++)
	{
		if(st[i]=='1')
		{
			x+=f[c[1]][0];
			y+=f[c[1]][1];
		}else
		if(st[i]=='2')
		{
			x+=f[c[2]][0];
			y+=f[c[2]][1];
		}else
		if(st[i]=='3')
		{
			x+=f[c[3]][0];
			y+=f[c[3]][1];
		}else
		if(st[i]=='0')
		{
			x+=f[c[4]][0];
			y+=f[c[4]][1];
		}
		if(x<=0||x>n||y<0||y>=m)break;
		if(s[x][y]=='#')break;
		if(s[x][y]=='E')
		{
			flag=true;
			break;
		}
	}
	if(flag)ans++;
}
void dfs(int k)
{
	if(k>4)
	{
		check();
		return;
	}
	for(int i=1;i<=4;i++)
		if(b[i]==0)
		{
			b[i]=1;
			c[k]=a[i];
			dfs(k+1);
			b[i]=0;
		}
}
int main()
{
	ans=0;
	f[1][0]=1;f[1][1]=0;
	f[2][0]=-1;f[2][1]=0;
	f[3][0]=0;f[3][1]=1;
	f[4][0]=0;f[4][1]=-1;
	scanf("%d%d\n",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%s\n",s[i]);
		for(int j=0;j<m;j++)
			if(s[i][j]=='S')
			{
				x1=i;y11=j;	
			}else
			if(s[i][j]=='E')
			{
				x2=i;y2=j;
			}
	}
	scanf("%s",st);
	for(int i=1;i<=4;i++)a[i]=i;
	memset(b,0,sizeof(b));dfs(1);
	cout<<ans<<endl;
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack-Oran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值