USACO Section 2.4 The Tamworth Two - 又犯2了..又把一个简单的问题整复杂了..

    做了好几个小时~~开始的做法是先找出C走的路径~~并记下循环节~~~然后再走F..走到一个点就判断下是不是能同时~~开了2个4维数组~~代码码了120多行~~结果还是没调出来...要吐了..

    刚才突然开窍了~~这不同时记步数同时走~~啥时候一样就ok了~~~呃~~十分钟解决~~矮油~~

Program:

/*  
ID: zzyzzy12  
LANG: C++  
TASK: ttwo
*/    
#include<iostream>    
#include<istream>
#include<stdio.h>    
#include<string.h>    
#include<math.h>    
#include<stack>
#include<algorithm>    
#include<queue> 
using namespace std;   
const int face[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
char mar[12][12];
int s[12][12][12][12],c[12][12][12][12];
int y,x,step,k,ty,tx,my,mx;
bool judge(int ty,int tx)
{
      if (ty==10 || ty<0 || tx==10 || tx<0 || mar[ty][tx]=='*') return false;
      return true;
} 
void choose(int *y,int *x,int *k)
{ 
      ty=*y+face[*k][0]; tx=*x+face[*k][1];
      if (!judge(ty,tx)) 
      {
             *k=(*k+1)%4; 
             return;
      }  
      *y=ty;  *x=tx;
      return;
}  
int getanswer()
{ 
     int step=0,ty,tx,x1,y1,x2,y2,k1=0,k2=0;
     for (y1=0;y1<10;y1++)
     {
          for (x1=0;x1<10;x1++)
             if (mar[y1][x1]=='C') break;
          if (x1!=10) break;   
     }
     for (y2=0;y2<10;y2++)
     {
          for (x2=0;x2<10;x2++)
             if (mar[y2][x2]=='F') break;
          if (x2!=10) break;   
     }
     while (step<10001)
     {
           step++;
           choose(&y1,&x1,&k1);
           choose(&y2,&x2,&k2);
           if (x1==x2 && y1==y2) return step;
     }
     return 0;
}
int main()
{
     freopen("ttwo.in","r",stdin);  
     freopen("ttwo.out","w",stdout);   
     for (int i=0;i<10;i++) scanf("%s",mar[i]); 
     printf("%d\n",getanswer());
     return 0;   
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值