usaco2.41The Tamworth Two(模拟)

刚开始输出0的时候想错了一地方 想着标记来着 看CZ的 数不大 直接大于多少时退出就好了

View Code
  1 /*
  2    ID: cuizhe
  3    LANG: C++
  4    TASK: ttwo
  5   */
  6 #include <iostream>
  7 #include<cstdio>
  8 #include<cstring>
  9 #include<algorithm>
 10 #include<stdlib.h>
 11 using namespace std;
 12 char str[12][12];
 13 int s1,s2,s3,s4,t1,t2;
 14 void judge(int x,int y,int t,int ff)
 15 {
 16     if(t==0)
 17     {
 18         x--;
 19         if(str[x][y]=='*'||x<0)
 20         {
 21             x++;
 22             t = (t+1)%4;
 23         }
 24     }
 25     else if(t==1)
 26     {
 27         y++;
 28         if(str[x][y]=='*'||y>=10)
 29         {
 30             y--;
 31             t = (t+1)%4;
 32         }
 33     }
 34     else if(t==2)
 35     {
 36         x++;
 37         if(str[x][y]=='*'||x>=10)
 38         {
 39             x--;
 40             t = (t+1)%4;
 41         }
 42     }
 43     else
 44     {
 45         y--;
 46         if(str[x][y]=='*'||y<0)
 47         {
 48             y++;
 49             t = (t+1)%4;
 50         }
 51     }
 52     if(ff==1)
 53     {
 54         s1 = x;
 55         s2 = y;
 56         t1 = t;
 57     }
 58     else
 59     {
 60         s3 = x;
 61         s4 = y;
 62         t2 = t;
 63     }
 64 }
 65 int main()
 66 {
 67     freopen("ttwo.in","r",stdin);
 68     freopen("ttwo.out","w",stdout);
 69     int i,j;
 70     for(i = 0; i < 10 ; i++)
 71     {
 72         for(j = 0 ; j < 10 ; j++)
 73         {
 74             cin>>str[i][j];
 75             if(str[i][j]=='C')
 76             {
 77                 s1 = i;
 78                 s2 = j;
 79             }
 80             if(str[i][j]=='F')
 81             {
 82                 s3 = i;
 83                 s4 = j;
 84             }
 85         }
 86         getchar();
 87     }
 88     t1 = 0;
 89     t2 = 0;
 90     int t = 1;
 91     while(1)
 92     {
 93         judge(s1,s2,t1,1);
 94         judge(s3,s4,t2,2);
 95         if(s1==s3&&s2==s4)
 96         {
 97             cout<<t<<endl;
 98             return 0;
 99         }
100         t++;
101         if(t>100010)
102         {
103             cout<<"0\n";
104             break;
105         }
106     }
107     return 0;
108 }
原文地址:https://www.cnblogs.com/shangyu/p/3026777.html