poj 2026 Borg Maze
2015-07-30 21:39
246 查看
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
Sample Output
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#define LL long long
#define INF 0x3f3f3f3f
#define MAX 51
using namespace std;
struct node
{
int x,y;
int step;
};
char str[MAX][MAX];
int ant;
int Map[MAX*MAX][MAX*MAX];
int a[MAX][MAX];
int eveco[MAX*MAX];
bool vis[MAX*MAX];
bool flag[MAX][MAX];
int n,m;
int top;
int Dir[][2] = {{0,1},{1,0},{-1,0},{0,-1}};
void BFS(int x,int y)
{
memset(flag,false,sizeof(flag));
queue<node >que;
node b,c;
b.x = x;
b.y = y;
b.step = 0;
flag[x][y] = true;
que.push(b);
while(!que.empty())
{
b = que.front();
que.pop();
if(a[b.x][b.y] != 0)
{
Map[a[x][y]][a[b.x][b.y]] = b.step;
}
for(int i=0; i<4; i++)
{
c.x = b.x + Dir[i][0];
c.y = b.y + Dir[i][1];
c.step = b.step + 1;
if(!flag[c.x][c.y] && str[c.x][c.y] != '#')
{
flag[c.x][c.y] = true;
que.push(c);
}
}
}
}
int Prim()
{
int cost = 0;
memset(vis,false,sizeof(vis));
for(int i=1; i<=top; i++)
{
eveco[i] = Map[1][i];
}
eveco[1] = 0;
vis[1] = true;
for(int i=1; i<top; i++)
{
int m = INF;
int pos;
for(int j=1; j<=top; j++)
{
if(!vis[j] && m > eveco[j])
{
pos = j;
m = eveco[j];
}
}
cost += m;
vis[pos] = true;
for(int j=1; j<=top; j++)
{
if(!vis[j] && eveco[j] > Map[pos][j])
{
eveco[j] = Map[pos][j];
}
}
}
return cost;
}
int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
top = 0;
memset(a,0,sizeof(a));
scanf("%d%d",&n,&m);
gets(str[0]);
for(int i=1; i<=n; i++)
{
gets(str[i]);
for(int j=1; j<=m; j++)
{
if(str[i][j] == 'S' || str[i][j] == 'A')
{
a[i][j] = ++top;
}
}
}
// for(int i=1;i<=top;i++)
// {
// for(int j=1;j<=top;j++)
// {
// if(j == top)
// cout<<a[i][j]<<endl;
// else
// cout<<a[i][j]<<" ";
// }
// }
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(a[i][j] != 0)
BFS(i,j);
}
}
printf("%d\n",Prim());
}
return 0;
}
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#define LL long long
#define INF 0x3f3f3f3f
#define MAX 51
using namespace std;
struct node
{
int x,y;
int step;
};
char str[MAX][MAX];
int ant;
int Map[MAX*MAX][MAX*MAX];
int a[MAX][MAX];
int eveco[MAX*MAX];
bool vis[MAX*MAX];
bool flag[MAX][MAX];
int n,m;
int top;
int Dir[][2] = {{0,1},{1,0},{-1,0},{0,-1}};
void BFS(int x,int y)
{
memset(flag,false,sizeof(flag));
queue<node >que;
node b,c;
b.x = x;
b.y = y;
b.step = 0;
flag[x][y] = true;
que.push(b);
while(!que.empty())
{
b = que.front();
que.pop();
if(a[b.x][b.y] != 0)
{
Map[a[x][y]][a[b.x][b.y]] = b.step;
}
for(int i=0; i<4; i++)
{
c.x = b.x + Dir[i][0];
c.y = b.y + Dir[i][1];
c.step = b.step + 1;
if(!flag[c.x][c.y] && str[c.x][c.y] != '#')
{
flag[c.x][c.y] = true;
que.push(c);
}
}
}
}
int Prim()
{
int cost = 0;
memset(vis,false,sizeof(vis));
for(int i=1; i<=top; i++)
{
eveco[i] = Map[1][i];
}
eveco[1] = 0;
vis[1] = true;
for(int i=1; i<top; i++)
{
int m = INF;
int pos;
for(int j=1; j<=top; j++)
{
if(!vis[j] && m > eveco[j])
{
pos = j;
m = eveco[j];
}
}
cost += m;
vis[pos] = true;
for(int j=1; j<=top; j++)
{
if(!vis[j] && eveco[j] > Map[pos][j])
{
eveco[j] = Map[pos][j];
}
}
}
return cost;
}
int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
top = 0;
memset(a,0,sizeof(a));
scanf("%d%d",&n,&m);
gets(str[0]);
for(int i=1; i<=n; i++)
{
gets(str[i]);
for(int j=1; j<=m; j++)
{
if(str[i][j] == 'S' || str[i][j] == 'A')
{
a[i][j] = ++top;
}
}
}
// for(int i=1;i<=top;i++)
// {
// for(int j=1;j<=top;j++)
// {
// if(j == top)
// cout<<a[i][j]<<endl;
// else
// cout<<a[i][j]<<" ";
// }
// }
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(a[i][j] != 0)
BFS(i,j);
}
}
printf("%d\n",Prim());
}
return 0;
}
相关文章推荐
- hdu-1213 How Many Tables
- 嵌入式软件开发——嵌入式软件常见的笔试题(c)
- 查看library_cache 库缓冲区的命中率
- 表空间、数据文件对应关系
- [CTCI] 子串判断
- cenyos7安装telnet服务器
- poj 1258 Agri-Net
- c++中的vector容器
- Android 当Service遇到Handler
- Linux_历史
- Observer模式
- js 构造函数
- 在云计算时代,如何监控云服务的 SLA ?
- 前端框架从头开始-浏览器的判定
- 来谈谈网络安全,关于Session冒名顶替和cookie防篡改的问题
- NYOJ 745 蚂蚁的难题(二)
- 嵌入式软件开发——CVTE 面试+笔试题
- [leetcode-43]Multiply Strings(java)
- Android中xml中shape的属性助记
- Linux或者Windows系统中鼠标点击偶尔无反应?