Home > Old Blog Posts > USACO 2.4.1 The Tamworth Two

USACO 2.4.1 The Tamworth Two


归到模拟题吧,注意判重就行了。

代码:

/*
ID: dementr1
PROG: ttwo
LANG: C++
*/
#include<iostream>
#include<fstream>
using namespace std;
ifstream fin(“ttwo.in”);
ofstream fout(“ttwo.out”);
int fx,fy,cx,cy;
bool walk[10][10][10][10][5][5]={false};
enum enummap{wall,place};
enummap map[10][10];
void init()
{
int i,j;
char tmp;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
fin>>tmp;
if(tmp==’*’) map[i][j]=wall;
else
{
map[i][j]=place;
if(tmp==’F’)
{
fx=i;
fy=j;
}
if(tmp==’C’)
{
cx=i;
cy=j;
}
}
}
}
void work()
{
int time=0,dirf=1,dirc=1;
while(1)
{
time++;
if(dirf==1)
{
if(fx>0&&map[fx-1][fy]!=wall) fx–;
else dirf=4;
}
else if(dirf==2)
{
if(fx<9&&map[fx+1][fy]!=wall) fx++;
else dirf=3;
}
else if(dirf==3)
{
if(fy>0&&map[fx][fy-1]!=wall) fy–;
else dirf=1;
}
else if(dirf==4)
{
if(fy<9&&map[fx][fy+1]!=wall) fy++;
else dirf=2;
}
if(dirc==1)
{
if(cx>0&&map[cx-1][cy]!=wall) cx–;
else dirc=4;
}
else if(dirc==2)
{
if(cx<9&&map[cx+1][cy]!=wall) cx++;
else dirc=3;
}
else if(dirc==3)
{
if(cy>0&&map[cx][cy-1]!=wall) cy–;
else dirc=1;
}
else if(dirc==4)
{
if(cy<9&&map[cx][cy+1]!=wall) cy++;
else dirc=2;
}
if(fx==cx&&fy==cy) break;
if(walk[fx][fy][cx][cy][dirf][dirc])
{
time=0;
break;
}
walk[fx][fy][cx][cy][dirf][dirc]=true;
}
fout<<time<<endl;
}
int main()
{
init();
work();
fin.close();
fout.close();
return 0;
}

Categories: Old Blog Posts
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: