1. 程式人生 > >nyoj 58-最少步數

nyoj 58-最少步數

最少步數

時間限制:3000 ms  |  記憶體限制:65535 KB 難度:4
描述

這有一個迷宮,有0~8行和0~8列:

 1,1,1,1,1,1,1,1,1
 1,0,0,1,0,0,1,0,1
 1,0,0,1,1,0,0,0,1
 1,0,1,0,1,1,0,1,1
 1,0,0,0,0,1,0,0,1
 1,1,0,1,0,1,0,0,1
 1,1,0,1,0,1,0,0,1
 1,1,0,1,0,0,0,0,1
 1,1,1,1,1,1,1,1,1

0表示道路,1表示牆。

現在輸入一個道路的座標作為起點,再如輸入一個道路的座標作為終點,問最少走幾步才能從起點到達終點?

(注:一步是指從一座標點走到其上下左右相鄰座標點,如:從(3,1)到(4,1)。)

輸入
第一行輸入一個整數n(0<n<=100),表示有n組測試資料;
隨後n行,每行有四個整數a,b,c,d(0<=a,b,c,d<=8)分別表示起點的行、列,終點的行、列。
輸出
輸出最少走幾步。
樣例輸入
2
3 1  5 7
3 1  6 7
樣例輸出
12
11

水題一道,無腦廣搜就過了吧,以前寫的,沒什麼可說的,有點對不起這個難度,現在看用stl程式碼可以減少一半以上

 
#include<stdio.h>
int map[9][9] = {1,1,1,1,1,1,1,1,1,
 1,0,0,1,0,0,1,0,1,
 1,0,0,1,1,0,0,0,1,
 1,0,1,0,1,1,0,1,1,
 1,0,0,0,0,1,0,0,1,
 1,1,0,1,0,1,0,0,1,
 1,1,0,1,0,1,0,0,1,
 1,1,0,1,0,0,0,0,1,
 1,1,1,1,1,1,1,1,1};
int queue[100][3] , haxi[9][9];
int head , tail;
int i , start_x , start_y , end_x , end_y;
void bfs()
{
	while(head != tail)
	{
		if(queue[head][0] > 0 && map[queue[head][0] - 1][queue[head][1]] == 0 && haxi[queue[head][0] - 1][queue[head][1]] == 0)
		{
			queue[tail][0] = queue[head][0] - 1;
			queue[tail][1] = queue[head][1];
			queue[tail][2] = queue[head][2] + 1;
			haxi[queue[tail][0]][queue[tail][1]] = 1;
			if(queue[tail][0] == end_x && queue[tail][1] == end_y)
			{
				printf("%d\n" , queue[tail][2]); 
				return ;
			}
			tail++;
		}
		if(queue[head][0] < 8 && map[queue[head][0] + 1][queue[head][1]] == 0 && haxi[queue[head][0] + 1][queue[head][1]] == 0)
		{
			queue[tail][0] = queue[head][0] + 1;
			queue[tail][1] = queue[head][1];
			queue[tail][2] = queue[head][2] + 1;
			haxi[queue[tail][0]][queue[tail][1]] = 1;
			if(queue[tail][0] == end_x && queue[tail][1] == end_y)
			{
				printf("%d\n" , queue[tail][2]); 
				return ;
			}
			tail++;
		}
		if(queue[head][1] > 0 && map[queue[head][0]][queue[head][1] - 1] == 0 && haxi[queue[head][0]][queue[head][1] - 1] == 0)
		{
			queue[tail][0] = queue[head][0];
			queue[tail][1] = queue[head][1] - 1;
			queue[tail][2] = queue[head][2] + 1;
			haxi[queue[tail][0]][queue[tail][1]] = 1;
			if(queue[tail][0] == end_x && queue[tail][1] == end_y)
			{
				printf("%d\n" , queue[tail][2]); 
				return ;
			}
			tail++;
		}
		if(queue[head][1] < 8 && map[queue[head][0]][queue[head][1] + 1] == 0  && haxi[queue[head][0]][queue[head][1] + 1] == 0)
		{
			queue[tail][0] = queue[head][0];
			queue[tail][1] = queue[head][1] + 1;
			queue[tail][2] = queue[head][2] + 1;
			haxi[queue[tail][0]][queue[tail][1]] = 1;
			if(queue[tail][0] == end_x && queue[tail][1] == end_y)
			{
				printf("%d\n" , queue[tail][2]); 
				return ;
			}
			tail++;
		}
		head++;
	}
	
}

int find_way(int x , int y)
{


	head = 0 ;
	tail = 1;
	queue[head][0] = start_x;
	queue[head][1] = start_y;
	haxi[queue[head][0]][queue[head][1]] = 1;
	bfs();
	if(head == tail)
		return 0;
	else return 1;
}
int main()
{

	
	int x,y;

	scanf("%d" , &i);
	while(i--)
	{
		scanf("%d %d %d %d" , &start_x , &start_y , &end_x , &end_y);
		for(x = 0 ; x < 9 ; x++)
			for(y = 0 ; y < 9 ; y++)
				haxi[x][y] = 0;
		if(find_way(start_x , start_y) == 0)
			printf("0\n");
	}
	return 0;
}