H - Fire Game

Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)

You can assume that the grass in the board would never burn out and the empty grid would never get fire.

Note that the two grids they choose can be the same.

Input
The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.

1 <= T <=100, 1 <= n <=10, 1 <= m <=10

Output
For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.

Sample Input
4
3 3
.#.

.#.
3 3
.#.
#.#
.#.
3 3

#.#

3 3

…#
#.#
Sample Output
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2

#include<queue>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int vis[100][100];
char mise[100][100];
typedef struct
{
	int x,y;
	int step;
}N;
N s,e;
N map[100];
int n,m,cas=1,ans;
int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void print()
{
	memset(mise,0,sizeof(mise));
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>mise[i][j];
}
int judge()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(mise[i][j]=='#'&&vis[i][j]==0)
				return 0;
		}
	}
	return 1;
}
int bfs(int x1,int y1,int x2,int y2)
{
	int ans=0;
	memset(vis,0,sizeof(vis));
	vis[x1][y1]=1;
	vis[x2][y2]=1;
	s.x=x1,s.y=y1;
	e.x=x2,e.y=y2;
	s.step=e.step=0;
     queue<N>q;
	 q.push(s);
	 q.push(e);
	 while(q.size())
	 {
        N now=q.front();
		q.pop();
		N next;
		if(ans<now.step)
			ans=now.step;
		for(int i=0;i<4;i++)
		{
            int a=now.x+d[i][0],b=now.y+d[i][1];
			if(vis[a][b]==0&&a>=1&&a<=n&&b>=1&&b<=m)
			{
				if(mise[a][b]=='#'){
			     next.x=a;
				 next.y=b;
				 next.step=now.step+1;
				 q.push(next);
				 vis[a][b]=1;}
			}
		}
	 }
	 return ans;
}
void solve()
{
	int ans=0x3f3f3f;
	int count=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(mise[i][j]=='#')
			{
				map[count].x=i;
				map[count].y=j;
				count++;
			}
		}
	}
	if(count<=2){
		cout<<"Case "<<cas<<": 0"<<endl;
	return ;}
	int temp;
    for(int i=0;i<count;i++)
	{
		for(int j=0;j<count;j++)
		{
			if(i==j) continue;
			 temp=bfs(map[i].x,map[i].y,map[j].x,map[j].y);
			if(judge())
			if(ans>temp)
			ans=temp;
		}
	}
	if(ans==0x3f3f3f)
		cout<<"Case "<<cas<<": -1"<<endl;
	else
		cout<<"Case "<<cas<<": "<<ans<<endl;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		print();
		solve();
		cas++;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的坦克大战游戏实现的代码示例。这个示例实现了基本的游戏逻辑和图形界面,您可以在其基础上做出更多的改进和优化。 ``` #include <iostream> #include <cstdlib> #include <ctime> #include <conio.h> using namespace std; const int WIDTH = 80; // 游戏界面宽度 const int HEIGHT = 25; // 游戏界面高度 class Tank { public: Tank(int x, int y, char ch) : x(x), y(y), ch(ch), dir(0), speed(1), bullets(10), power(1) {} void move(); void fire(); void show(); bool hit(int px, int py); friend class Game; private: int x; int y; char ch; int dir; int speed; int bullets; int power; }; void Tank::move() { if (_kbhit()) { int c = _getch(); switch (c) { case 'a': dir = 0; x -= speed; break; case 'd': dir = 1; x += speed; break; case 'w': dir = 2; y -= speed; break; case 's': dir = 3; y += speed; break; } } if (x < 0) x = 0; if (x >= WIDTH) x = WIDTH - 1; if (y < 0) y = 0; if (y >= HEIGHT) y = HEIGHT - 1; } void Tank::fire() { if (bullets <= 0) { cout << "No bullets left!" << endl; return; } bullets--; switch (dir) { case 0: cout << "Bullet fired to left." << endl; break; case 1: cout << "Bullet fired to right." << endl; break; case 2: cout << "Bullet fired to up." << endl; break; case 3: cout << "Bullet fired to down." << endl; break; } } void Tank::show() { cout << ch; } bool Tank::hit(int px, int py) { if (px == x && py == y) { cout << "Tank hit!" << endl; return true; } return false; } class Game { public: Game(); void start(); private: Tank player; Tank enemy; }; Game::Game() : player(0, 0, 'P'), enemy(WIDTH - 1, HEIGHT - 1, 'E') {} void Game::start() { srand(time(NULL)); while (true) { system("cls"); player.move(); player.show(); enemy.show(); if (enemy.hit(player.x, player.y)) { cout << "You lose!" << endl; break; } if (rand() % 100 < 10) { enemy.fire(); } if (player.hit(enemy.x, enemy.y)) { cout << "You win!" << endl; break; } Sleep(50); } } int main() { Game game; game.start(); return 0; } ``` 这个示例实现了一个基本的坦克大战游戏,玩家可以使用键盘控制坦克移动和发射炮弹,敌方坦克会随机发射炮弹并追踪玩家坦克。当玩家坦克和敌方坦克碰撞时游戏结束,玩家需要尽可能地摧毁敌方坦克以获得胜利。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值