USACO 2.4.1 The Tamworth Two 题解

【算法】模拟  【难度】★☆☆☆☆


直接模拟。
注意如果牛和人开始绕圈子会陷入死循环,所以在时间足够大的时候退出,具体的临界值我选了10000000,nocow上有证明说最多160000步

【收获】极限情况判断

View Code
  1 /*
2 ID: wsc5001
3 LANG: C
4 TASK: ttwo
5 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 struct wz{int x,y,fs;};
9 int ct=0;
10 int map[12][12];// 0=可通过√ 1=墙x
11 struct wz moveit(struct wz c)
12 {
13 struct wz ans;
14 if (c.fs==0)//
15 {
16 if (map[c.x-1][c.y]==1||map[c.x-1][c.y]==-1)
17 {
18 ans=c;
19 ans.fs=1;
20 return ans;
21 }
22 else
23 {
24 ans=c;
25 ans.x=c.x-1;
26 return ans;
27 }
28 }
29 if (c.fs==1)//
30 {
31 if (map[c.x][c.y+1]==1||map[c.x][c.y+1]==-1)
32 {
33 ans=c;
34 ans.fs=2;
35 return ans;
36 }
37 else
38 {
39 ans=c;
40 ans.y=c.y+1;
41 return ans;
42 }
43 }
44 if (c.fs==2)//
45 {
46 if (map[c.x+1][c.y]==1||map[c.x+1][c.y]==-1)
47 {
48 ans=c;
49 ans.fs=3;
50 return ans;
51 }
52 else
53 {
54 ans=c;
55 ans.x=c.x+1;
56 return ans;
57 }
58 }
59 if (c.fs==3)//
60 {
61 if (map[c.x][c.y-1]==1||map[c.x][c.y-1]==-1)
62 {
63 ans=c;
64 ans.fs=0;
65 return ans;
66 }
67 else
68 {
69 ans=c;
70 ans.y=c.y-1;
71 return ans;
72 }
73 }
74 }
75 int main()
76 {
77 freopen("ttwo.in","r",stdin);
78 freopen("ttwo.out","w",stdout);
79 int i,j,tms=0;
80 char temp;
81 struct wz f,c,sf,sc;
82 for (i=0;i<12;i++)
83 for (j=0;j<12;j++)
84 map[i][j]=-1;
85 for (i=1;i<=10;i++)
86 {
87 for (j=1;j<=10;j++)
88 {
89 scanf("%c",&temp);
90 if(temp=='.') map[i][j]=0;
91 if(temp=='*') map[i][j]=1;
92 if(temp=='C') {map[i][j]=0;c.x=i;c.y=j;}
93 if(temp=='F') {map[i][j]=0;f.x=i;f.y=j;}
94 }
95 scanf("%c",&temp);
96 }
97 sf.x=f.x; sf.y=f.y;
98 sc.x=c.x; sc.y=c.y;
99
100 c.fs=0; f.fs=0;
101
102 while (ct<=10000000)
103 {
104 c=moveit(c);
105 f=moveit(f);
106 ct++;
107 if (c.x==f.x && c.y==f.y)
108 {printf("%d\n",ct);break;}
109 if (c.x==sc.x && c.y==sc.y && f.x==sf.x && f.y==sf.y)
110 tms++;
111 if (tms==4)
112 {printf("0\n");break;}
113 }
114 if (ct>10000000)printf("0\n");
115 //system("pause");
116 fclose(stdin);
117 fclose(stdout);
118 }



posted @ 2012-03-25 19:40  wsc500  阅读(183)  评论(0编辑  收藏  举报