Codevs P1116 四色问题
题目描述 Description
给定N(小于等于8)个点的地图,以及地图上各点的相邻关系,请输出用4种颜色将地图涂色的所有方案数(要求相邻两点不能涂成相同的颜色)
数据中0代表不相邻,1代表相邻
输入输出
输入描述 Input Description
第一行一个整数n,代表地图上有n个点
接下来n行,每行n个整数,每个整数是0或者1。第i行第j列的值代表了第i个点和第j个点之间是相邻的还是不相邻,相邻就是1,不相邻就是0.
我们保证a[i][j] = a[j][i] (a[i,j] = a[j,i])
输出描述 Output Description
染色的方案数
样例 Sample
样例输入 Sample Input
8
0 0 0 1 0 0 1 0
0 0 0 0 0 1 0 1
0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
1 0 1 0 0 0 0 0
0 1 0 0 0 0 0 0
样例输出 Sample Output
15552
数据范围及提示 Data Size & Hint
n<=8
题目分析
该题为深度搜索题目,只要连好边即可。、
代码如下
program p1116;
type point=^rec;
rec=record
e:integer;
s:point;
end;
var n,x,i,j:integer;
ans:longint;
vertex:array[1..8] of point;
colour:array[1..8] of 0..4;
procedure insert(s,e:integer);
var p:point;
begin
new(p);
p^.e:=e;
p^.s:=vertex[s];
vertex[s]:=p;
end;
function check(x,k:integer):boolean;
var p:point;
begin
p:=vertex[x];
while p<>nil do
begin
if colour[p^.e]=k
then exit(false);
p:=p^.s;
end;
exit(true);
end;
procedure dfs(step:integer);
var k:integer;
begin
for k:=1 to 4 do
begin
if check(step,k)
then
begin
colour[step]:=k;
if step<n
then
dfs(step+1);
if step=n then ans:=ans+1;
colour[step]:=0;
end;
end;
end;
begin
readln(n);
for i:=1 to n do
for j:=1 to n do
begin
read(x);
if x=1
then
begin
insert(i,j);
end;
end;
for i:=1 to n do colour[i]:=0;
ans:=0;
dfs(1);
write(ans);
end.
测试详情
测试通过 Accepted
总耗时: 3 ms
0 / 0 数据通过测试.
运行结果
测试点#dt.in 结果:AC 内存使用量: 256kB 时间使用量: 1ms
测试点#dt1.in 结果:AC 内存使用量: 256kB 时间使用量: 1ms
测试点#dt2.in 结果:AC 内存使用量: 256kB 时间使用量: 1ms
加油,嘿
2016年高考倒计时2016高考时间:2016年6月7、8、9日
目前距离2016年高考还有240 天 12 时 53 分