meaning:题意是给一张图,图中有牛的位置,农场主的位子和障碍物。牛和农场主只能沿着一个方向走直到要出界或者走到障碍物就转弯!!(注意是走到障碍物才转弯,而不是碰到之前转弯),转弯的方向为顺时针,问农场主是否可以和牛相遇。
the reason of failure:1、需要注意是走到障碍物才转弯。
2、给的图有C和F,如果只遇到'.'才走下去显然是错的,遇到C||F都可以接着走下去。
3、判定不能走的条件不仅是他们在之前走过的位子,还要在这个位子面对的方向与之前相同。
thinking:直接按着题意模拟走就行,认真看题再码,别着急
代码:
/*
ID: me
PROG: ttwo
LANG: C++
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct ttt{
int r[2];
int c[2];
int d[2];
int step;
int l[2];
};
char map1[12][12];
int walked[11][11][11][11][11][11];
int dd(ttt a,int b){
if(a.r[b]>=1&&a.r[b]<=10&&a.c[b]>=1&&a.c[b]<=10)return 1;
else return 0;
}
int dd2(char c){
if(c=='.'||c=='F'||c=='C')return 1;
else return 0;
}
int main(){
freopen("ttwo.in","r",stdin);
freopen("ttwo.out","w",stdout);
int i,j,k,l,f1,f2,f3,t71,t2,t3;
ttt s1,s2;
memset(map1,0,sizeof(map1));
memset(walked,0,sizeof(walked));
for(i=1;i<=10;i++)
for(j=1;j<=10;j++){
cin >> map1[i][j];
if(map1[i][j]=='F'){
s1.r[0]=i;
s1.c[0]=j;
}else if(map1[i][j]=='C'){
s1.r[1]=i;
s1.c[1]=j;
}
}
s1.step=0;
s1.d[0]=0;
s1.d[1]=0;
queue<ttt>qq;
qq.push(s1);
while(!qq.empty()){
s1=qq.front();qq.pop();
//cout << s1.r[0] << "," << s1.c[0] << "-" <<s1.d[0] << " "
//<<s1.r[1] << "," << s1.c[1] << "-" << s1.d[1];
if(s1.r[0]==s1.r[1]&&s1.c[0]==s1.c[1]){
break;
}
//cout << " 没出去 ";
//if(s1.l[0]==0){
if(s1.d[0]==0){
if(dd2(map1[s1.r[0]][s1.c[0]])&&dd2(map1[s1.r[0]-1][s1.c[0]])){
s1.r[0]--;
}else{
s1.d[0]=(s1.d[0]+1)%4;
}
}else if(s1.d[0]==3){
if(dd2(map1[s1.r[0]][s1.c[0]])&&dd2(map1[s1.r[0]][s1.c[0]-1])){
s1.c[0]--;
}else{
s1.d[0]=(s1.d[0]+1)%4;
}
}else if(s1.d[0]==2){
if(dd2(map1[s1.r[0]][s1.c[0]])&&dd2(map1[s1.r[0]+1][s1.c[0]])){
s1.r[0]++;
}else{
s1.d[0]=(s1.d[0]+1)%4;
}
}else if(s1.d[0]==1){
if(dd2(map1[s1.r[0]][s1.c[0]])&&dd2(map1[s1.r[0]][s1.c[0]+1])){
s1.c[0]++;
}else{
s1.d[0]=(s1.d[0]+1)%4;
}
}
//if(s1.l[1]==0){
if(s1.d[1]==0){
if(dd2(map1[s1.r[1]][s1.c[1]])&&dd2(map1[s1.r[1]-1][s1.c[1]])){
s1.r[1]--;
}else{
s1.d[1]=(s1.d[1]+1)%4;
}
}else if(s1.d[1]==3){
if(dd2(map1[s1.r[1]][s1.c[1]])&&dd2(map1[s1.r[1]][s1.c[1]-1])){
s1.c[1]--;
}else{
s1.d[1]=(s1.d[1]+1)%4;
}
}else if(s1.d[1]==2){
if(dd2(map1[s1.r[1]][s1.c[1]])&&dd2(map1[s1.r[1]+1][s1.c[1]])){
s1.r[1]++;
}else{
s1.d[1]=(s1.d[1]+1)%4;
}
}else if(s1.d[1]==1){
if(dd2(map1[s1.r[1]][s1.c[1]])&&dd2(map1[s1.r[1]][s1.c[1]+1])){
s1.c[1]++;
}else{
s1.d[1]=(s1.d[1]+1)%4;
}
}
// cout << s1.r[0] << "," << s1.c[0] << "-" <<s1.d[0] << " "
// <<s1.r[1] << "," << s1.c[1] << "-" << s1.d[1] << " " ;
// cout << walked[s1.r[0]][s1.c[0]][s1.d[0]][s1.r[1]][s1.c[1]][s1.d[1]] << endl;
if(walked[s1.r[0]][s1.c[0]][s1.d[0]][s1.r[1]][s1.c[1]][s1.d[1]]==0){
walked[s1.r[0]][s1.c[0]][s1.d[0]][s1.r[1]][s1.c[1]][s1.d[1]]=s1.step;
s1.step++;
// cout << " 接着走" <<s1.step << endl;
qq.push(s1);
}
}
//cout << endl;
if(s1.r[0]==s1.r[1]&&s1.c[0]==s1.c[1]){
cout << s1.step << endl;
}else{
cout << 0 <<endl;
}
return 0;
}