程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj 3026 Borg Maze

poj 3026 Borg Maze

編輯:C++入門知識

Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7722 Accepted: 2592

Description

The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual is linked to the collective by a sophisticated subspace network that insures each member is given constant supervision and guidance.

Your task is to help the Borg (yes, really) by developing a program which helps the Borg to estimate the minimal cost of scanning a maze for the assimilation of aliens hiding in the maze, by moving in north, west, east, and south steps. The tricky thing is that the beginning of the search is conducted by a large group of over 100 individuals. Whenever an alien is assimilated, or at the beginning of the search, the group may split in two or more groups (but their consciousness is still collective.). The cost of searching a maze is definied as the total distance covered by all the groups involved in the search together. That is, if the original group walks five steps, then splits into two groups each walking three steps, the total distance is 11=5+3+3.

Input

On the first line of input there is one integer, N <= 50, giving the number of test cases in the input. Each test case starts with a line containg two integers x, y such that 1 <= x,y <= 50. After this, y lines follow, each which x characters. For each character, a space `` '' stands for an open space, a hash mark ``#'' stands for an obstructing wall, the capital letter ``A'' stand for an alien, and the capital letter ``S'' stands for the start of the search. The perimeter of the maze is always closed, i.e., there is no way to get out from the coordinate of the ``S''. At most 100 aliens are present in the maze, and everyone is reachable.

Output

For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive.

Sample Input

2
6 5
##### 
#A#A##
# # A#
#S  ##
##### 
7 7
#####  
#AAA###
#    A#
# S ###
#     #
#AAA###
#####  

Sample Output

8
11

最小生成樹+bfs(求個點之間距離)

注意數組稍微開大一點,還有空格問題。。

#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
#define N 200         //N為100就wrong了
#define M 200
const int inf=10000;
int map[N][N];         //記錄個點之間距離
char str[N][N];
int row,col;          //行和列
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
struct node
{
	int x,y,t;
	friend bool operator<(node a,node b)
	{                            //距離短的優先級高
		return a.t>b.t;
	}
}f[M];
int judge(int x,int y)        
{
	if(x>=0&&x=0&&yq;
	node cur,next;
	cur.x=f[index].x;
	cur.y=f[index].y;
	cur.t=0;
	q.push(cur);
	memset(mark,0,sizeof(mark));
	mark[cur.x][cur.y]=1;
	while(!q.empty())
	{
		cur=q.top();
		q.pop();
		for(i=0;imap[index][i])
				dis[i]=map[index][i];
	}
	return 0;
}
int main()
{
	int T,i,j,n;
	char temp[200];
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&col,&row);
		gets(temp);               //空格會有多個
		for(i=0;i


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved