中国海洋大学第四届朗讯杯高级组 A 2718 Rocky(模拟)

题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2718

题意:优先直走,右 左 后。。。。

思路:

我定义的朝向 已经 d[]的先后次序。。。。。

  3  
2   1
  4  

 

 

 

 

  1 #include <cstring>
  2 #include <cstdio>
  3 using namespace std;
  4 
  5 int dx[5]={0,1,-1,0,0};
  6 int dy[5]={0,0,0,1,-1};
  7 struct node
  8 {
  9     int x,y,tow,step;
 10 }pos,before;
 11 int main()
 12 {
 13     int i,col,row,r;
 14     int G[22][22];
 15     int x,y,sx,sy,f,count=1;
 16     while(~scanf("%d%d%d",&col,&row,&r)&&(col||row||r))
 17     {
 18          memset(G,0,sizeof(G));
 19         for(i=0; i<r; i++)
 20         {
 21             scanf("%d%d",&x,&y);
 22             G[x][y]=1;
 23         }
 24         scanf("%d%d",&sx,&sy);
 25         if(sx==1)
 26         f=1;
 27         else if(sx==col)
 28         f=2;
 29         else if(sy==1)
 30         f=3;
 31         else if(sy==row)
 32         f=4;
 33         pos.step=1; pos.tow=f;
 34         pos.x=sx; pos.y=sy;
 35         for(i=1; ;i++)
 36         {
 37             if(pos.tow==1)
 38             {
 39                 if(pos.x>=col)
 40                 break;
 41                 if(G[pos.x+dx[1]][pos.y+dy[1]]==0)
 42                 {
 43                     pos.x=pos.x+dx[1];
 44                     pos.y=pos.y+dy[1];
 45                     pos.step++;
 46                 }
 47                 else if(G[pos.x+dx[4]][pos.y+dy[4]]==0)
 48                 {
 49                     pos.x=pos.x+dx[4];
 50                     pos.y=pos.y+dy[4];
 51                     pos.step++;
 52                     pos.tow=4;
 53                 }
 54                 else if(G[pos.x+dx[3]][pos.y+dy[3]]==0)
 55                 {
 56                     pos.x=pos.x+dx[3];
 57                     pos.y=pos.y+dy[3];
 58                     pos.step++;
 59                     pos.tow=3;
 60                 }
 61                 else if(G[pos.x+dx[2]][pos.y+dy[2]]==0)
 62                 {
 63                     pos.x=pos.x+dx[2];
 64                     pos.y=pos.y+dy[2];
 65                     pos.step++;
 66                     pos.tow=2;
 67                 }
 68             }
 69             else if(pos.tow==2)
 70             {
 71                 if(pos.x<=1)
 72                 break;
 73                 if(G[pos.x+dx[2]][pos.y+dy[2]]==0)
 74                 {
 75                     pos.x=pos.x+dx[2];
 76                     pos.y=pos.y+dy[2];
 77                     pos.step++;
 78                 }
 79                 else if(G[pos.x+dx[3]][pos.y+dy[3]]==0)
 80                 {
 81                     pos.x=pos.x+dx[3];
 82                     pos.y=pos.y+dy[3];
 83                     pos.step++;
 84                     pos.tow=3;
 85                 }
 86                 else if(G[pos.x+dx[4]][pos.y+dy[4]]==0)
 87                 {
 88                     pos.x=pos.x+dx[4];
 89                     pos.y=pos.y+dy[4];
 90                     pos.step++;
 91                     pos.tow=4;
 92                 }
 93                 else if(G[pos.x+dx[1]][pos.y+dy[1]]==0)
 94                 {
 95                     pos.x=pos.x+dx[1];
 96                     pos.y=pos.y+dy[1];
 97                     pos.step++;
 98                     pos.tow=1;
 99                 }
100             }
101             else if(pos.tow==3)
102             {
103                 if(pos.y>=row)
104                 break;
105                 if(G[pos.x+dx[3]][pos.y+dy[3]]==0)
106                 {
107                     pos.x=pos.x+dx[3];
108                     pos.y=pos.y+dy[3];
109                     pos.step++;
110                 }
111                 else if(G[pos.x+dx[1]][pos.y+dy[1]]==0)
112                 {
113                     pos.x=pos.x+dx[1];
114                     pos.y=pos.y+dy[1];
115                     pos.step++;
116                     pos.tow=1;
117                 }
118                 else if(G[pos.x+dx[2]][pos.y+dy[2]]==0)
119                 {
120                     pos.x=pos.x+dx[2];
121                     pos.y=pos.y+dy[2];
122                     pos.step++;
123                     pos.tow=2;
124                 }
125                 else if(G[pos.x+dx[4]][pos.y+dy[4]]==0)
126                 {
127                     pos.x=pos.x+dx[4];
128                     pos.y=pos.y+dy[4];
129                     pos.step++;
130                     pos.tow=4;
131                 }
132             }
133             else if(pos.tow==4)
134             {
135                 if(pos.y<=1)
136                 break;
137                 if(G[pos.x+dx[4]][pos.y+dy[4]]==0)
138                 {
139                     pos.x=pos.x+dx[4];
140                     pos.y=pos.y+dy[4];
141                     pos.step++;
142                 }
143                 else if(G[pos.x+dx[2]][pos.y+dy[2]]==0)
144                 {
145                     pos.x=pos.x+dx[2];
146                     pos.y=pos.y+dy[2];
147                     pos.step++;
148                     pos.tow=2;
149                 }
150                 else if(G[pos.x+dx[1]][pos.y+dy[1]]==0)
151                 {
152                     pos.x=pos.x+dx[1];
153                     pos.y=pos.y+dy[1];
154                     pos.step++;
155                     pos.tow=1;
156                 }
157                 else if(G[pos.x+dx[3]][pos.y+dy[3]]==0)
158                 {
159                     pos.x=pos.x+dx[3];
160                     pos.y=pos.y+dy[3];
161                     pos.step++;
162                     pos.tow=3;
163                 }
164             }
165             if(pos.x>=1&&pos.x<=col&&pos.y>=1&&pos.y<=row)
166             {
167                 before.x=pos.x; before.y=pos.y;
168             }
169         }
170         printf("Case %d: ",count++);
171         if(pos.x>=1&&pos.x<=col&&pos.y>=1&&pos.y<=row)
172         printf("%d %d %d\n",pos.x,pos.y,pos.step);
173         else
174         printf("%d %d %d\n",before.x,before.y,pos.step-1);
175     }
176     return 0;
177 }

 

 

posted @ 2013-12-01 20:41  水门  阅读(323)  评论(0编辑  收藏  举报