Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <queue>
- using namespace std;
- typedef struct node {
- int x,y;
- } node_t;
- int graph[1010][1010], visited[1010][1010], level[1010][1010];
- int main() {
- int row, column;
- while (scanf("%d %d", &row, &column)) {
- if (!row && !column) return 0;
- int num,i,j;
- for (i=0;i<row; i++)
- for (j=0; j<column; j++) {
- graph[i][j] = 0;
- visited[i][j] = 0;
- }
- scanf("%d", &num);
- while (num--) {
- int r, a,b;
- scanf("%d %d", &r, &b);
- while (b--) {
- scanf("%d", &a);
- graph[r][a] = -1;
- }
- }
- node_t src, dst;
- scanf("%d %d %d %d", &src.x, &src.y, &dst.x, &dst.y);
- queue <node_t> myq;
- myq.push(src);
- level[src.x][src.y] = 0;
- while (!myq.empty()) {
- int cx = myq.front().x;
- int cy = myq.front().y;
- if (cx == dst.x && cy == dst.y) break;
- node_t cur;
- myq.pop();
- visited[cx][cy] = 1;
- if (cx+1<column) {
- if (graph[cx+1][cy] == 0 && !visited[cx+1][cy]) {
- cur.x = cx+1;
- cur.y = cy;
- myq.push(cur);
- level[cur.x][cur.y] = level[cx][cy] + 1;
- visited[cur.x][cur.y] = 1;
- }
- }
- if (cx-1 >=0) {
- if (graph[cx-1][cy] == 0&& !visited[cx-1][cy]) {
- cur.x = cx-1;
- cur.y = cy;
- myq.push(cur);
- level[cur.x][cur.y] = level[cx][cy] + 1;
- visited[cur.x][cur.y] = 1;
- }
- }
- if (cy+1 < row){
- if (graph[cx][cy+1] == 0&& !visited[cx][cy+1]) {
- cur.x = cx;
- cur.y = cy+1;
- myq.push(cur);
- level[cur.x][cur.y] = level[cx][cy] + 1;
- visited[cur.x][cur.y] = 1;
- }
- }
- if (cy-1 >= 0) {
- if (graph[cx][cy-1] == 0&& !visited[cx][cy-1]) {
- cur.x = cx;
- cur.y = cy-1;
- myq.push(cur);
- level[cur.x][cur.y] = level[cx][cy] + 1;
- visited[cur.x][cur.y] = 1;
- }
- }
- }
- printf("%d\n", level[dst.x][dst.y]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment