Codeforces Gym 100650C The Game of Efil DFS
The Game of Efil
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88443#problem/C
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 generation. • 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.
HINT
题意
给你一个病毒产生或者消亡的规律,然后让你求这个状态下的图的上一状态一共有多少种
题解:
数据范围很小,n*m<=16,所以直接dfs出所有的状态就好了
然后再check
代码:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 200051 #define mod 10007 #define eps 1e-9 int Num; //const int inf=0x7fffffff; //нчоч╢С const int inf=0x3f3f3f3f; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************** int M[30][30]; int d[30][30]; int cnt[30][30]; int d2[30][30]; int n,m; int ans=0; int dx[8]={1,-1,1,-1,1,-1,0,0}; int dy[8]={1,-1,-1,1,0,0,1,-1}; int C(int x,int k) { if(x==-1) x=k-1; if(x==k) return 0; return x; } int check() { memset(cnt,0,sizeof(cnt)); memset(d2,0,sizeof(d2)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { for(int k=0;k<8;k++) { int xx=C(i+dx[k],n); int yy=C(j+dy[k],m); if(d[xx][yy]==1) cnt[i][j]++; } } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(d[i][j]==1) { if(cnt[i][j]==2||cnt[i][j]==3) d2[i][j]=1; else d2[i][j]=0; } else { if(cnt[i][j]==3) d2[i][j]=1; } } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(d2[i][j]!=M[i][j]) return 0; } } return 1; } int tot=0; void dfs(int x,int y) { if(x==n) { if(check()) ans++; return; } d[x][y]=0; if(y==m-1) dfs(x+1,0); else dfs(x,y+1); d[x][y]=1; if(y==m-1) dfs(x+1,0); else dfs(x,y+1); d[x][y]=0; } int main() { int t=1; while(cin>>n>>m) { if(n==0&&m==0) break; memset(M,0,sizeof(M)); ans=0; int k=read(); for(int i=0;i<k;i++) { int x=read(),y=read(); M[x][y]=1; } dfs(0,0); if(ans!=0) printf("Case %d: %d possible ancestors.\n",t++,ans); else printf("Case %d: Garden of Eden.\n",t++); } }