zl程序教程

您现在的位置是:首页 >  其它

当前栏目

龙与虫..

..
2023-09-27 14:26:25 时间

题目描述
​ 给出一张 n∗m 的地图,在地图上有一只虫,样子却很像龙,而且嘴能快速的直射喷出一种毒液,瞬间杀死敌人。

​ 现在假设虫的初始位置在 x1,y1,另外在 x2,y2 处有一个敌人。假设虫移动一步需要单位 1 的时间,而杀死敌人不需要时间,并且虫的毒液射程无穷大,但毒液不能穿透障碍物,虫可以向四个方向移动,向八个方向攻击,求虫最少需要多少时间才能消灭敌人。

输入
​ 第一行两个整数 n,m。(1≤n,m≤128)
​ 接下来是一个 n∗m 的矩阵,O 表示空地,X 表示障碍物。

​ 再接下来每行对应一组数据,对于每组数据,一行四个整数分别表示 x2,y2,x1,y1,数据不多于十组。

​ 读入 0,0,0,0 表示程序结束。

输出
​ 对于每组数据,输出一行一个数,表示消灭敌人的最短时间,若无法消灭敌人,则输出 Impossible!。

样例输入

3 4
OXXO
XXOO
XOOO
3 2 2 4
3 3 1 1
0 0 0 0

样例输出
1
Impossible!
数据规模与约定
​ 时间限制:1 s

​ 内存限制:256 M

​ 100% 的数据保证 1≤n,m≤128

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

struct node {
	int x, y, step;
};
int check[150][150];
int n, m;
char map[150][150];
int dir[8][2] = { 0, 1, 1, 0, 0, -1, -1, 0,
	1, 1, 1, -1, -1, 1, -1, -1 };


int func() {
	int a, b, c, d;
	cin >> c >> d >> a >> b;
	if (!a) return 0;
	memset(check, 0, sizeof(check));
	
	for (int i = 0; i < 8; i++) {
		for (int j = 1; 1; j++) {
			int x = c + j * dir[i][0];
			int y = d + j * dir[i][1];
			if (map[x][y] != 'O') break;
			check[x][y] = 2;
		}
	}
	check[c][d] = 2;
	if (check[a][b] == 2) {
		cout << 0 << endl;
		return 1;
	}
	queue<node> que;
	que.push({ a,b,0 });
	check[a][b] = 1;
	while (!que.empty()) {
		node temp = que.front();
		que.pop();
		for (int i = 0; i < 4; i++) {
			int x = temp.x + dir[i][0];
			int y = temp.y + dir[i][1];
			if (check[x][y] == 2) {
				cout << temp.step + 1 << endl;
				return 1;
			}
			if (map[x][y] == 'O' && check[x][y] != 1) {
				check[x][y] = 1;
				que.push({ x, y, temp.step + 1 });
			}
		}
	}
	cout << "Impossible!" << endl;
	return 1;
}
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> & map[i][1];
	}
	while (1) {
		if (func() == 0) {
			break;
		}
	}
	return 0;
}