ZOJ 10th Anniversary Contest - A

//搜索
//策略:先对横轴扩展,得可能解横轴区域,再验证纵轴。对纵轴相同操作
 1 #include<stdio.h>
2 int save[310][310];
3 int main ()
4 {
5 int n,m;
6 while (scanf ("%d%d",&n,&m)!=EOF)
7 {
8 int t;
9 scanf ("%d",&t);
10 int i,j,p;
11 for (i=0;i<n;i++)
12 for (j=0;j<m;j++)
13 save[i][j]=0;
14 for (p=0;p<t;p++)
15 {
16 int x,y;
17 scanf ("%d%d",&x,&y);
18 save[x][y]=1;
19 }
20 scanf ("%d",&t);
21 int mark=60;
22 for (p=0;p<t;p++)
23 {
24 int x1,y1,x2,y2;
25 scanf ("%d%d%d%d",&x1,&y1,&x2,&y2);
26 int find=0;
27 if (save[x1][y1]==1&&save[x2][y2]==1) //对横向处理
28 { // begin1、end1为x1的左右可扩展区域
29 int begin1=x1,end1=x1,begin2=x2,end2=x2; // begin2、end2为x2的左右可扩展区域
30 while (save[begin1-1][y1]==0&&begin1-1>=0) begin1--; //begin、end为共同的扩展区域
31 while (save[end1+1][y1]==0&&end1+1<n) end1++;
32 while (save[begin2-1][y2]==0&&begin2-1>=0) begin2--;
33 while (save[end2+1][y2]==0&&end2+1<n) end2++;
34 int begin,end;
35 if (begin1>begin2) begin=begin1;
36 else begin=begin2;
37 if (end1<end2) end=end1;
38 else end=end2;
39 if (begin<=end)
40 {
41 for (i=begin;i<=end&&find==0;i++)
42 {
43 int yes=0;
44 int big,small;
45 if (y1>y2){big=y1;small=y2;}
46 else {big=y2;small=y1;}
47 for (j=small+1;j<big;j++)
48 if (save[i][j]==1) {yes=1;break;}
49 if (yes==0) find=1;
50 }
51 }
52 if (find==0) //同理对纵向处理
53 {
54 begin1=y1;end1=y1;begin2=y2;end2=y2;
55 while (save[x1][begin1-1]==0&&begin1-1>=0) begin1--;
56 while (save[x1][end1+1]==0&&end1+1<m) end1++;
57 while (save[x2][begin2-1]==0&&begin2-1>=0) begin2--;
58 while (save[x2][end2+1]==0&&end2+1<m) end2++;
59 if (begin1>begin2) begin=begin1;
60 else begin=begin2;
61 if (end1<end2) end=end1;
62 else end=end2;
63 if (begin<=end)
64 {
65 for (j=begin;j<=end&&find==0;j++)
66 {
67 int yes=0;
68 int big,small;
69 if (x1>x2){big=x1;small=x2;}
70 else {big=x2;small=x1;}
71 for (i=small+1;i<big;i++)
72 if (save[i][j]==1) {yes=1;break;}
73 if (yes==0) find=1;
74 }
75 }
76 }
77 }
78 if (find==0) mark--;
79 else
80 {
81 mark+=2;
82 save[x1][y1]=0;
83 save[x2][y2]=0;
84 }
85 printf ("%d\n",mark);
86 }
87 }
88 return 0;
89 }

 

posted @ 2012-03-11 19:02  ustc-acm  阅读(454)  评论(2编辑  收藏  举报