您的位置:首页 > 其它

NOIP2013 Day2 T3 华容道

2017-07-19 21:26 309 查看
//拍60分暴力当练手了,千万别把已经输入的数据初始化了!!!
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
const int xx[] = {0, 0, 1, -1};
const int yy[] = {1, -1, 0, 0};
int n, m, q;
int map[33][33];
bool v[33][33][33][33];
int tx, ty;
struct node{
int ex, ey, sx, sy;
int step;
};

queue<node> haoyu;

void init(){
//memset(map, 0, sizeof(map));
memset(v, 0, sizeof(v));
while(!haoyu.empty())   haoyu.pop();
}

bool judge(int ex, int ey, int sx, int sy){
if(ex < 1 || ey < 1 || ex > n || ey > m || !map[ex][ey] || !map[sx][sy])    return 0;
if(v[ex][ey][sx][sy])   return 0;
v[ex][ey][sx][sy] = 1;
return 1;
}

void bfs(){
while(!haoyu.empty()){
node u = haoyu.front(); haoyu.pop();
for(int i = 0; i < 4; i++){
int nx = u.sx;
int ny = u.sy;
int vx = u.ex + xx[i];
int vy = u.ey + yy[i];
if(nx == vx && ny == vy)    nx = u.ex, ny = u.ey;
if(judge(vx, vy, nx, ny)){
haoyu.push({vx, vy, nx, ny, u.step+1});
if(nx == tx && ny == ty){
cout << u.step+1 << endl;
return;
}
}
}
}
cout << "-1" << endl;
return;
}

int main(){
cin >> n >> m >> q;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> map[i][j];
/*for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++)
cout << map[i][j] << " ";
cout << endl;
}*/
//cout << map[1][2] << endl;
while(q--){
init();
int ex, ey, sx, sy;
cin >> ex >> ey >> sx >> sy >> tx >> ty;
if(sx == tx && sy == ty){
cout << "0" << endl;
return 0;
}
haoyu.push({ex, ey, sx, sy, 0});
v[ex][ey][sx][sy] = 1;
bfs();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: