您的位置:首页 > 其它

SGU239(搜索?)

2016-04-20 21:26 363 查看
Minesweeper

time limit per test: 0.25 sec.

memory limit per test: 4096 KB

input: standard

output: standard

Not so long ago, an International Cell Playing Company (ICPC) decided to organize an Amusing Championship of Minesweeper (ACM). “Minesweeper” is a standard Windows game (you can find it now at Start->Programs->Games->Minesweeper or somewhere like this). The rules of “Minesweeper” are the following: you are to open all the cells of a game field, that don’t contain mines, and to mark by flags all the cells, that contain mines. During the game, numbers which are written in the cells help you to solve this task. These numbers appear in the opened cells (it is denied to open a cell with a mine, in this case you will be exploded). A number written in a cell indicates how many mines are situated in 8 neighboring cells (fig.1).

fig. 1

Just for aesthetic reasons “0” is indicated in background color, so you don’t see it. In the case of absence of any mines in the cells neighboring with some opened cell, all neighboring cells will be opened automatically (you can see it on the first figure, where only 1 cell was opened by player, and other cells were opened automatically).

Now we can immediately say where the mines are certainly situated (fig. 2).

fig. 2

Very often a following situation takes place. No cell of the first column is opened, while all the cells of second column are opened, and it is guaranteed that second and third columns don’t contain mines at all (fig. 3).

fig. 3

In that case the numbers shown in the cells of the second column indicate exactly the number of mines situated in the neighboring cells of the first column. Such situations are very hard for players to solve, and that’s why your friend who wants to train for ACM, asked you to write a program that gives him a possibility to check his solutions.

Imagine now, that we are playing Minesweeper on the field, containing N rows and 2 columns. It is guaranteed, that the second column is free of mines and completely opened. The first column is not opened yet, and may contain mines. You are to determine the location of all mines in the first column, when you know all the data of the second column. It is possible to do so if, for example, one of the cells of the second column contains number 3 (fig. 4).

fig. 4

So you can be sure, that all 3 neighboring cells of the first column contain mines. Therefore it is not difficult to find out, which cells are free of mines (fig. 5).

fig. 5

Now, using this information, we can say all about the first column (the remaining cells definitely contain mines). Sometimes it can be impossible to determine surely, where the mines are situated, and where not (fig. 6).

fig. 6

Sometimes the field may be mistaken at all, and there exists no mine distribution that produces such numbers in the second column (fig. 3, pay attention to the upper part of the column). It is not very amazing when you remember who has written this game.

Now you must write a program which, given the data of the second column, outputs how many distributions of the mines in the first column exist, that produce such data in the second column.

Input

The first line of input contains integer N - height of the field (1<=N<=1000). Then N lines follow, each containing an integer ai (0<=ai<=3) - the number, written on the I-th cell of the second column.

Output

You must output the only number - an answer to the problem.

Sample test(s)

Input

Test #1

9

1 1 3 2 1 1 1 2 1

Test #2

2

1 1

Test #3

7

1 2 3 2 2 2 2

Output

Test #1

0

Test #2

2

Test #3

1

题目敢不敢再长一点。。。就是扫雷的玩儿法,只不过现在格子只有两列,而且他把一列全都点开了,而且都是数字,问你另一列雷的情况有几种。枚举第一个格子的情况你会发现接下去的所有格子都是确定的。比如最坏的情况所有点开的格子全都是1,在知道第一个格子的情况下后面所有格子的情况都可以推出来,既然1都可以那么2,3肯定都可以了。哦对了题目里说已经给你点好的那一列里出现的只有可能是1,2,3.另外也有可能出现死局答案为0的情况。

#include "cstring"
#include "cstdio"
#include "iostream"
#include "string.h"
#define mine 2
#define nomine 1
int a[1005];
int mark[1005];
int n;
int ans;
void dfs(int step)
{
if(step==n+1)
{
ans++;
return;
}
int cnt=0;
if(step-1>=1&&step-1<=n)
{
if(mark[step-1]==mine)
cnt++;
}
if(step>=1&&step<=n)
{
if(mark[step]==mine)
cnt++;
}
if(step+1>=1&&step+1<=n)
{
if(mark[step+1]==mine)
cnt++;
else
{
if(cnt<a[step])
{
mark[step+1]=mine;
cnt++;
}
}
}
if(cnt!=a[step])
return;
dfs(step+1);
}
int main()
{
while(~scanf("%d",&n))
{
ans=0;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
memset(mark,0,sizeof(mark));
mark[1]=mine;
dfs(1);
memset(mark,0,sizeof(mark));
mark[1]=nomine;
dfs(1);
printf("%d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: