原题:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1412
时间:2014.2.1
类型:回溯,dfs
源码:
#include<iostream>
#include<cstdio>
using namespace std;
int a[21][21],n;
int c[21],cnt=0;
bool pd=false;
void search(int s)
{
if(s==n+1) { cnt++; return; }
for(int i=1;i<=4;i++)
{
for(int j=1;j<=n;j++)
{
if(a[s][j]== 1 && c[j]==i ) { pd=true; break; }
}
if(!pd)
{
c[s]=i;
search(s+1);
}
else pd=false;
}
c[s]=0;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
search(1);
cout<<cnt;
return 0;
}
最后状态:AC
总结:一开始没想到pd这种方法,就感觉不对,但是不知道为什么不对