soj1171 The Game of Efil

原创 2016年05月30日 13:10:15

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.


题目大意:

生命游戏相关的题目,周围的邻居数目决定了下一代的个体存活与否,注意一下有限的地图内是wrap around的,即卷在一起的曲面,即左右其实相邻,上下也相邻,还有四个角也相邻,刚开始还以为这个面板是可以旋转的,旋转相等也算,结果似乎我想复杂了。问当前状态有多少种可能的父代会得到它。

解题方法:

额因为面板很小,最多16个格子而已,所以枚举父代格子的所有情况,一一判断是否会生成一个与输入相同的儿子就好了,枚举用dfs记录所有格子的状态。注意这里要上下左右相连,那么就重新定义一个大一圈的地图,将最右边一列赋给0列,将第一列赋给m+1列,上下同理,别忘了还有四个角,然后就对里面的每一个点按游戏规则进行判断存活与否就好了

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m;
int map[20][20];
int mm[20][20];
int ans[20][20];
int ansnum;
void dfs(int now){
	if (now==n*m){
		memset(map,0,sizeof(map));
		//memset(tmp,0)
		for (int i=1;i<=n;i++){
			for (int j=1;j<=m;j++){
				map[i][j]=mm[i-1][j-1];
				//cout<<map[i][j];
			}
			//cout<<endl;
		}
		//上下左右的连接
		for(int i=1;i<=m;i++){
			map[0][i]=map[n][i];
		}
		for(int i=1;i<=m;i++){
			map[n+1][i]=map[1][i];
		} 
		for (int i=1;i<=n;i++){
			map[i][0]=map[i][m];
		}
		for (int i=1;i<=n;i++){
			map[i][m+1]=map[i][1];
		}
		//四个角
		map[0][0]=map[n][m];
		map[0][m+1]=map[n][1];
		map[n+1][0]=map[1][m];
		map[n+1][m+1]=map[1][1];
	
		int tmp[20][20]={0};
		for (int i=1;i<=n;i++){
			for (int j=1;j<=m;j++){
				int sum=map[i-1][j-1]+map[i][j-1]+map[i+1][j-1]
						+map[i-1][j]+map[i+1][j]
						+map[i-1][j+1]+map[i][j+1]+map[i+1][j+1];
				if (map[i][j]==1){
					if (sum==3||sum==2){
						tmp[i][j]=1;
					}
					else tmp[i][j]=0;	
				}
				else if(sum==3) tmp[i][j]=1;
			}
		}
		for (int i=1;i<=n;i++){
			for (int j=1;j<=m;j++){
				if (tmp[i][j]!=ans[i][j]){
					return ;
				}
			}
		}
		ansnum++;
		return;
	}
	//深搜枚举所有情况
	int row=now/m;
	int col=now%m;
	mm[row][col]=1;
	dfs(now+1);
	mm[row][col]=0;
	dfs(now+1);
	 
} 
int main(){
	int co=1;
	while(cin>>n>>m){
		if (n==0) break;
		int num;
		cin>>num;
		ansnum=0;
		int x,y;
		memset(ans,0,sizeof(ans));
		memset(mm,0,sizeof(mm));
		for (int i=0;i<num;i++){
			cin>>x>>y;
			ans[x+1][y+1]=1;
		}
		dfs(0);
		printf("Case %d: ",co++);
		if (ansnum==0){
			printf("Garden of Eden.\n");
		}
		else {
			printf("%d possible ancestors.\n",ansnum);
		}
	}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Sicily 1171. The Game of Efil 【利用完全二叉树遍历构造序列】

题目链接在此 源代码: #include #include int total; int finalGrid[20][20]; const int neiRow[] = { -1, 0, 1,...
  • CharlieLincy
  • CharlieLincy
  • 2014年12月24日 14:54
  • 1029

HDU 2053 Switch Game(开灯问题,唯一分解定理)

Switch Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To...
  • hurmishine
  • hurmishine
  • 2016年05月08日 19:23
  • 2050

UVA11624 Fire! 两次BFS 读懂题意很重要

Fire! Joe works in a maze. Unfortunately,portions of the maze have caught on fire, and the owner of ...
  • wr132
  • wr132
  • 2015年04月30日 21:17
  • 1735

Sicily 1171. The Game of Efil

// DFS #include #include #include using namespace std; int m, n; int res; int dx[8] = { -1, -1,...
  • u013038899
  • u013038899
  • 2014年12月22日 18:16
  • 371

Codeforces Gym 100650C The Game of Efil DFS

题目链接:http://codeforces.com/gym/100650/attachments题意:每个细胞如果四周细胞太少了,就会孤独而死,如果细胞周围细胞太多了,就会挤死。给你个布局,问你他的...
  • yxg_123
  • yxg_123
  • 2017年02月27日 20:39
  • 119

Codeforces Gym100650C The Game of Efil

Problem C: The Game of EfilAlmost anyone who has ever taken a class in computer science is famil...
  • a456052
  • a456052
  • 2015年08月19日 11:03
  • 220

poj 1171 Letter Game 字符串处理或字符串背包

#include using namespace std; char s[16]; int cnt[256]; int tot[256]; int m[256]; struct DICT { ch...
  • sepNINE
  • sepNINE
  • 2015年05月20日 13:47
  • 838

【RMQ】区间求最值 SOJ2436 Picture puzzle game

RMQ即范围最小值问题(Range Minimum Query)
  • lth404391139
  • lth404391139
  • 2014年10月07日 23:36
  • 552

SOJ.Dice Game

1000. Dice Game     总提交数量: 100 通过数量: 59                 时间限制:1秒   ...
  • a576699534
  • a576699534
  • 2015年03月29日 19:33
  • 383

SOJ 1052. Candy Sharing Game

1052. Candy Sharing Game Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description ...
  • chengyangyy
  • chengyangyy
  • 2013年08月25日 22:44
  • 688
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:soj1171 The Game of Efil
举报原因:
原因补充:

(最多只允许输入30个字)