[题目来源]:vijos@潘帕斯雄鹰生日模拟赛T2
[关键字]:搜索
[题目大意]:给出n个不规则图形,要求用所有的图形拼成一个正方形是否可行,可行则输出方案。
//=====================================================================================================
[分析]:虽然一眼就能看出搜索但是要想实现却并不是太简单。就是枚举每一个图形可以放在哪,然后放置在搜索下一个。判断时要利用给出的矩形判断。
[代码]:
1 type
2 rec = record
3 x, y: longint;
4 map: array[0..200,0..200] of longint;
5 end;
6 var
7 n, s, bc: longint;
8 c: array[0..20,0..20] of word;
9 a: array[0..200] of rec;
10
11 procedure print(x: longint);
12 var
13 i, j: longint;
14 begin
15 if x = -1 then
16 begin
17 writeln('N');
18 close(input);
19 close(output);
20 halt;
21 end;
22 if x = -2 then
23 begin
24 writeln(1);
25 close(input);
26 close(output);
27 halt;
28 end;
29 for i := 1 to bc do
30 begin
31 for j := 1 to bc do write(c[i,j]);
32 writeln;
33 end;
34 close(input);
35 close(output);
36 halt;
37 end;
38
39 procedure init;
40 var
41 i, j, k: longint;
42 ch: char;
43 begin
44 readln(n);
45 for k := 1 to n do
46 begin
47 readln(a[k].x,a[k].y);
48 for i := 1 to a[k].x do
49 begin
50 for j := 1 to a[k].y do
51 begin
52 read(ch);
53 a[k].map[i,j] := ord(ch)-48;
54 inc(s,ord(ch)-48);
55 end;
56 readln;
57 end;
58 end;
59 bc := trunc(sqrt(s));
60 if bc <> sqrt((s)) then print(-1);
61 if s = 1 then print(-2);
62 end;
63
64 function cleck(x1, y1, w: longint):boolean;
65 var
66 i, j, x, y: longint;
67 begin
68 if a[w].x+x1-1 > bc then exit(false);
69 if a[w].y+y1-1 > bc then exit(false);
70 for i := x1 to a[w].x+x1-1 do
71 for j := y1 to a[w].y+y1-1 do
72 if (a[w].map[i-x1+1,j-y1+1] = 1) then
73 if c[i,j] <> 0 then exit(false);
74 exit(true);
75 end;
76
77 procedure put(x1 ,y1, w: longint);
78 var
79 i, j: longint;
80 begin
81 for i := x1 to x1+a[w].x-1 do
82 for j := y1 to y1+a[w].y-1 do
83 if a[w].map[i-x1+1,j-y1+1] = 1 then c[i,j] := w;
84 end;
85
86 procedure dfs(k: longint);
87 var
88 i, j: longint;
89 t: array[0..20,0..20] of word;
90 begin
91 if k > n then print(1);
92 // writeln(k);
93 for i := 1 to bc do
94 for j := 1 to bc do
95 if cleck(i,j,k) then
96 begin
97 t := c;
98 put(i,j,k);
99 dfs(k+1);
100 c := t;
101 end;
102 end;
103
104 begin
105 assign(input,'c:\1.in');reset(input);
106 assign(output,'c:\1.out');rewrite(output);
107 init;
108 dfs(1);
109 // writeln('asdf');
110 print(-1);
111 readln(n);
112 end.