FZU 2150 求双搜最优解

http://acm.fzu.edu.cn/problem.php?pid=2150



#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<sstream>
#include<time.h>
#include<utility>
#include<malloc.h>

using namespace std;

int t, n, m;
char b[15][15];
int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, 1 }, { 0, -1 } };
int vis[15][15];
int check(int x, int y)
{
	if (x >= 0 && x < n && y >= 0 && y < m)
		return 1;
	return 0;
}
struct node
{
	int x;
	int y;
	int t;
}q[150];

queue<node> de;
int step = 0;

int main()
{
	scanf("%d",&t);
	int c = 1;
	while (t--)
	{
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n; i++)
			scanf("%s",b[i]);

		int num = 0;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
			{
				if (b[i][j] == '#')
				{
					q[num].x = i;
					q[num++].y = j;
				}
			}

		int ans = 0x3f3f3f3f;
		if (num == 2)
			ans = 0;
		else 
		for (int i = 0; i < num; i++)
		{
			for (int j = i; j < num; j++)
			{
				int sx = q[i].x;
				int sy = q[i].y;
				int ex = q[j].x;
				int ey = q[j].y;
				{

					while (!de.empty()) de.pop();
					
					node qq, qqq;
					qq.x = sx; qq.y = sy; qq.t = 0;
					qqq.x = ex; qqq.y = ey; qqq.t = 0;

					de.push(qq);
					de.push(qqq);
					memset(vis,0,sizeof(vis));

					vis[sx][sy] = vis[ex][ey] = 1;

					step = 0;

					/
					while (!de.empty())
					{
						node qq = de.front();
						de.pop();
							step = qq.t;
						for (int i = 0; i < 4; i++)
						{
							int x = qq.x + dir[i][0];
							int y = qq.y + dir[i][1];

							if (!check(x, y) || vis[x][y] || b[x][y] == '.')
								continue;
							node qqq;
							qqq.x = x;
							qqq.y = y;
							qqq.t = qq.t + 1;
							vis[x][y] = 1;
							de.push(qqq);
						}
					}
					
					int ok = 1;

					for (int i = 0; i < n; i++)
					{
						for (int j = 0; j < m; j++)
						{
							if (vis[i][j] == 0 && b[i][j] == '#')
							{
								ok = 0;
								break;
							}
						}
					}

					if (ok)
					{
						ans = min(ans, step);
					}
				}
			}
		}
		if (ans == 0x3f3f3f3f)
			ans = -1;
		printf("Case %d: %d\n",c++,ans);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值