社区讨论
33玄关
P1518[USACO2.4] 两只塔姆沃斯牛 The Tamworth Two参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @lvg43jil
- 此快照首次捕获于
- 2024/04/26 11:27 2 年前
- 此快照最后确认于
- 2024/04/26 17:04 2 年前
CPP
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
struct DIR{
int x,y;
}dir;
queue<DIR> quman,qucow;
char mp[15][15];
int main(){
int x=-1,y=0;//北
quman.push({x,y});
qucow.push({x,y});
x=0,y=1;//东
quman.push({x,y});
qucow.push({x,y});
x=1,y=0;//南
quman.push({x,y});
qucow.push({x,y});
x=0,y=-1;//西
quman.push({x,y});
qucow.push({x,y});
for(int i=1;i<=10;i++){
for(int j=1;j<=10;j++){
cin>>mp[i][j];
}
}
//找到牛和人的位置
int cowx,cowy,perx,pery;
for(int i=1;i<=10;i++){
for(int j=1;j<=10;j++){
if(mp[i][j]=='F'){
perx=i; pery=j;
}
if(mp[i][j]=='C'){
cowx=i; cowy=j;
}
}
}
int time=0;
//牛和人移动
while(!(perx==cowx&&pery==cowy)){
int tx=perx+quman.front().x;
int ty=pery+quman.front().y;
while(mp[tx][ty]=='*'||tx<1||ty<1||tx>10||ty>10){
//超出边界或者遇到障碍
int xx=quman.front().x;
int yy=quman.front().y;
quman.pop();
quman.push({xx,yy}); //换方向
tx=perx+quman.front().x;
ty=pery+quman.front().y;
}
perx=tx; pery=ty;
int dx=cowx+qucow.front().x;
int dy=cowy+qucow.front().y;
while(mp[dx][dy]=='*'||dx<1||dy<1||dx>10||dy>10){
//超出边界或者遇到障碍
int xx=qucow.front().x;
int yy=qucow.front().y;
qucow.pop();
qucow.push({xx,yy}); //换方向
dx=cowx+qucow.front().x;
dy=cowy+qucow.front().y;
}
cowx=dx; cowy=dy;
time++;
if(time>1000000){ //超出时间限制当成永远不可能相遇
cout<<0;
return 0;
}
}
cout<<time;
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...