本文主要是介绍zju 1008 Gnome Tetravex dfs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008
check() 如果在k不是在最左边就水平两个三角形比较。不是在最上边就垂直两个三角形比较。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
int a[30][4];
int b[25],c[25];//c类型数,b记录当前放置的格子下标。
int type,n,flag;
bool check(int k)
{if(k>=n&&a[b[k]][0]!=a[b[k-n]][2])return false;if(k%n!=0&&a[b[k]][3]!=a[b[k-1]][1])return false;return true;
}
void dfs(int x)
{if(x == n*n){flag = 1;return ;}for(int i=0;i<type;i++){if(flag == 1)return ;if(c[i]>0){c[i]--;b[x]=i;if(check(x))dfs(x+1);c[i]++;}}
}
int main()
{int count=0;int top,right,left,bottom;while(cin>>n){type=flag=0;if(n==0)break;count++;memset(c,0,sizeof(c));memset(b,0,sizeof(b));for(int i=0;i<n*n;i++){cin>>top>>right>>bottom>>left;int j=0;while(j<type){if(a[j][0]==top&&a[j][1]==right&&a[j][2]==bottom&&a[j][3]==left){c[j]++;break;}j++;}if(j == type){a[type][0] = top;a[type][1] = right;a[type][2] = bottom;a[type][3] = left;c[type]=1;type++;}}dfs(0);if(count>1)cout<<endl;if(flag == 1)cout<<"Game "<<count<<": Possible"<<endl;elsecout<<"Game "<<count<<": Impossible"<<endl;}
}
这篇关于zju 1008 Gnome Tetravex dfs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!