| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
没有往回走,有大神知道为什么会超时吗!!!#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int dx[]={0, 0, 0, 1, -1}, dy[]={0, 1, -1, 0 , 0}; //计算流星和附近四个点的偏移
int map[400][400];
int n; //流星的数量
struct node{
int x, y, t;
};
int bfs(){
if(map[0][0]==0)return -1;
if(map[0][0]==-1)return 0;
queue<node>q;
q.push(node{0,0,0});
while(!q.empty()){
//cout<<'+';
node cur=q.front();
q.pop();
int x=cur.x, y=cur.y, t=cur.t;
cout<<"---------------"<<x<<y<<t<<endl;
if(map[x][y]==-1)return t;
int cx, cy;
for(int i=1;i<=4;i++){
cx=x+dx[i];
cy=y+dy[i];
if(cx>=0&&cy>=0&&(t+1<map[cx][cy]||map[cx][cy]==-1)){
if(map[cx][cy]!=-1)map[cx][cy]=t+1;
q.push(node{cx, cy, t+1});
cout<<'+'<<cx<<cy<<t+1<<map[cx][cy]<<endl;
}
}
}
return -1;
}
int bfs1(){
if(map[0][0]==0)return -1;
if(map[0][0]==-1)return 0;
node temp, now;
queue<node>que;
que.push(temp);
while(!que.empty()){
now=que.front();
que.pop();
for(int j=1;j<=4;j++){
temp.x=now.x+dx[j];
temp.y=now.y+dy[j];
temp.t=now.t+1;
if (temp.x>=0&&temp.x<=400&&temp.y>=0&&temp.y<=400){
if(map[temp.x][temp.y]==-1) return temp.t;
if(map[temp.x][temp.y]>temp.t) {
map[temp.x][temp.y]=temp.t;
que.push(temp);
}
}
}
}
return -1;
}
int main(){
int meteor_x, meteor_y, t;
memset(map, -1, sizeof(map)); //把坐标系的点初始化-1, 表示没有损坏
cin>>n; //流星的数目
for(int i=1;i<=n;i++){ //遍历每颗流星
cin>>meteor_x>>meteor_y>>t; //遍历每颗流星和他上下左右四个点
for(int j=0;j<5;j++){
int destroy_x = meteor_x + dx[j];
int destroy_y = meteor_y + dy[j];
if(destroy_x>=0&&destroy_x<=400&&destroy_y>=0&&destroy_y<=400&&(map[destroy_x][destroy_y]>t||map[destroy_x][destroy_y]==-1))
map[destroy_x][destroy_y]=t;
}
}
cout<<bfs()<<endl;
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator