您的位置:首页 > 其它

sicily 1171. The Game of Efil

2015-10-24 15:40 239 查看

1171. The Game of Efil

Constraints

Time Limit: 2 secs, Memory Limit: 64 MB

Description

Almost anyone who has ever taken a class in computer science is familiar with the "Game of Life," John Conway's cellular automata with extremely simple rules of birth, survival, and death that can give rise to astonishing
complexity.

The game is played on a rectangular field of cells, each of which has eight neighbors (adjacent cells). A cell is either occupied or not. The rules for deriving a generation from the previous one are:

If an occupied cell has 0, 1, 4, 5, 6, 7, or 8 occupied neighbors, the organism dies (0, 1: of loneliness; 4 thru 8: of overcrowding).

If an occupied cell has two or three occupied neighbors, the organism survives to the next gener-ation.

If an unoccupied cell has three occupied neighbors, it becomes occupied (a birth occurs).

One of the major problems researchers have looked at over the years is the existence of so-called "Garden of Eden" configurations in the Game of Life -- configurations that could not have arisen as the result of the application of the rules to some previous
configuration. We're going to extend this question, which we'll call the "Game of Efil": Given a starting configuration, how many possible parent configurations could it have? To make matters easier, we assume a finite grid in which edge and corner cells "wrap
around" (i.e., a toroidal surface). For instance, the 2 by 3 configuration:



has exactly three possible parent configurations; they are:



You should note that when counting neighbors of a cell, another cell may be counted as a neighbor more than once, if it touches the given cell on more than one side due to the wrap around. This is the case for the configurations above.

Input

There will be multiple test cases. Each case will start with a line containing a pair of positive integers m and n, indicating the number of rows and columns of the configuration, respectively. The next line will
contain a nonnegative integer k indicating the number of "live" cells in the configuration. The following k lines each contain the row and column number of one live cell, where row and column numbering both start at zero. The final test case is followed by
a line where m = n = 0 -- this line should not be processed. You may assume that the product of m and n is no more than 16.

Output

For each test case you should print one line of output containing the case number and the number of possible ancestors. Imitate the sample output below. Note that if there are 0 ancestors, you should print out

Garden of Eden.

Sample Input


2 3
2
0 0
0 1
3 3
4
0 0
0 1
0 2
1 1
3 3
5
0 0
1 0
1 2
2 1
2 2
0 0

Sample Output


Case 1: 3 possible ancestors.
Case 2: 1 possible ancestors.
Case 3: Garden of Eden.


题目分析

生命游戏,按照规则确定给定状态有几种父状态

遍历所有的初始状态,查看其子状态,

暴力深搜

#include <cstdio>
#include <iostream>
#include <memory.h>

int father[20][20];
int mid[20][20];
int son[20][20];
int row, col;
int count;
int dir[8][2] = {-1,-1, -1,0, -1,1, 0,-1, 0,1, 1,-1, 1,0, 1,1};
void getChild() {
for (int i = 1; i <= row; ++i) {
for (int j = 1; j <= col; ++j) {
int num = 0;
for (int k = 0; k < 8; ++k)
num += father[i+dir[k][0]][j+dir[k][1]];
if (father[i][j]) {
if (num == 2 || num == 3)
mid[i][j] = 1;
else
mid[i][j] = 0;
} else {
if (num == 3)
mid[i][j] = 1;
else
mid[i][j] = 0;
}
}
}
}

bool check() {
getChild();
for (int i = 1; i <= row; ++i)
for (int j = 1; j <= col; ++j)
if (mid[i][j] != son[i][j])
return false;
return true;
}

void print() {
for (int i = 0; i <= row+1; ++i) {
for (int j = 0; j <= col+1; ++j) {
printf("%d", father[i][j]);
}
printf("\n");
}
}

void travel(int r, int c) {
for (int digit = 0; digit < 2; ++digit) {
father[r][c] = digit;

for (int i = 1; i <= row; ++i) {
father[i][0] = father[i][col];
father[i][col + 1] = father[i][1];
}
for (int i = 0; i <= col + 1; ++i) {
father[0][i] = father[row][i];
father[row + 1][i] = father[1][i];
}

if (r == row && c == col) {
if (check()) {
count++;
//print();
}
} else {
int newr = c==col ? r+1 : r;
int newc = c==col ? 1 : c+1;
travel(newr, newc);
}
}
}

int main()
{
int id = 1;
while (scanf("%d%d", &row, &col)) {
if (row==0 && col==0)
break;
memset(father, 0, sizeof(father));
memset(mid, 0, sizeof(mid));
memset(son, 0, sizeof(son));
int lifes;
scanf("%d", &lifes);
int pr, pc;
for (int i = 0; i < lifes; ++i) {
scanf("%d%d", &pr, &pc);
son[pr+1][pc+1] = 1;
}
count = 0;
travel(1,1);

if (count == 0)
printf("Case %d: Garden of Eden.\n", id++);
else
printf("Case %d: %d possible ancestors.\n", id++, count);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: