Prime Path&Dungeon Master题解

一,Prime Path

题目出处

考点:搜索&贪心
题目大意:
我们需要从给出的数字开始,通过改变四位数上的某个数字(改变之后要求四位数仍然是素数)计算需要至少多少次改变。

题解·:
我们已知两个数,普遍想法就是把每一位上的数改变再判断是否为素数,最后再输出改变的次数(搜索),我们需要求出最小的次数,就要从高位开始(贪心),因此首先从高位开始变化数字,然后依次。
注意:在多次输入之前先把从1000到9999之间的素数求出来。用prime【x】表示,1表示不为素数,0表示为素数。
主要码源:

		for (int i = 1; i <= 9; i++)
		{
			k = i * 1000 + temp.px % 1000;
			if (flag[k] == 0 && prime[k] == 0)
			{
				p.px = k;
				p.step = temp.step + 1;
				flag[k] = 1;
				if (p.px == y)
				{
					printf("%d\n", p.step);
					return;
				}
				temp_1.push(p);
			}
		}
		for (int i = 0; i <= 9; i++)
		{
			k =(temp.px/1000)*1000+i*100 + temp.px % 100;
			if (flag[k] == 0 && prime[k] == 0)
			{
				p.px = k;
				p.step = temp.step + 1;
				flag[k] = 1;
				if (p.px == y)
				{
					printf("%d\n", p.step);
					return;
				}
				temp_1.push(p);
			}
		}
		for (int i = 0; i <= 9; i++)
		{
			k = (temp.px / 100) * 100 + i * 10 + temp.px % 10;
			if (flag[k] == 0 && prime[k] == 0)
			{
				p.px = k;
				p.step = temp.step + 1;
				flag[k] = 1;
				if (p.px == y)
				{
					printf("%d\n", p.step);
					return;
				}
				temp_1.push(p);
			}
		}
		for (int i = 0; i <= 9; i++)
		{
			k = (temp.px / 10) * 10 + i;
			if (flag[k] == 0 && prime[k] == 0)
			{
				p.px = k;
				p.step = temp.step + 1;
				flag[k] = 1;
				if (p.px == y)
				{
					printf("%d\n", p.step);
					return;
				}
				temp_1.push(p);
			}
		}

二,Dungeon Master

题目出处

考点:搜索
题目大意:
求在一个空间里,从S到E的最短路。这里的难点是了解三维空间内如何进行运动搜索。

题解:
用一个三维数组表示:s [ l ][ x ][ y ];   l表示层数,x表示行,y表示列;
建立一个走法:向右 ——>向左 ——>向上

int dx[6] = { 1,0,-1,0 ,0,0 };
int dy[6] = { 0,1,0,-1,0,0 };
int dz[6] = { 0,0,0,0,1,-1 };
temp.x = ss.x + dx[i];
temp.y = ss.y + dy[i];
temp.z = ss.z + dz[i];
temp.min = ss.min + 1;

注意:在输入之后一定要清空数组!!!!

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<climits>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<map>
#include<queue>
#include<vector>
#include<set>

using namespace std;
#define ll long long
#define MAX 10e7+3
#define MIN 1e-9

char s[100][100][100];
bool v[100][100][100];

struct step {
	int x;
	int y;
	int z;
	int min;
}a[70000], temp, ss;

int sx, sy, sz, flag;
int l, x, y;
int dy[6] = { 0,1,0,-1,0,0 };
int dx[6] = { 1,0,-1,0 ,0,0 };
int dz[6] = { 0,0,0,0,1,-1 };


void bfs()
{
	a[0].x = sx;
	a[0].y = sy;
	a[0].z = sz;
	a[0].min = 0;
	v[sz][sx][sy] = 1;
	int top = 1;
	int head = 0;
	while (head < top)
	{
		ss = a[head];
		head++;
		for (int i = 0; i <= 5; i++)
		{
			temp.x = ss.x + dx[i];
			temp.y = ss.y + dy[i];
			temp.z = ss.z + dz[i];
			temp.min = ss.min + 1;
			if (s[temp.z][temp.x][temp.y] == 'E')
			{
				flag = temp.min;
				printf("Escaped in %d minute(s).\n", flag);
				return;
			}
			if (s[temp.z][temp.x][temp.y] == '.' && temp.x >=0 && temp.x < x && temp.y >= 0 && temp.y < y && temp.z >= 0 && temp.z < l && v[temp.z][temp.x][temp.y] == 0)
			{
				v[temp.z][temp.x][temp.y] = 1;
				a[top] = temp;
				top++;
			}
		}
	}
	if (flag == 0)
	{
		cout << "Trapped!" << endl;
	}
}

int main(int argc, char* argv[])
{
	while (~scanf("%d%d%d", &l, &x, &y) && (l + x + y))
	{
		memset(v, 0,sizeof(v));
		memset(s, 0, sizeof(s));
		getchar();

		//输入矩阵
		for (int k = 0; k <l; k++)
		{
			for (int i = 0; i < x; i++)
			{
				for (int j = 0; j < y; j++)
				{
					scanf("%c", &s[k][i][j]);
					if (s[k][i][j] == 'S')
					{
						sz = k;
						sx = i;
						sy = j;
					}
				}
				getchar();
			}
			getchar();
		}
	/*	for (int i = 0; i < l; i++)
		{
			for (int j = 0; j < x; j++)
			{
				cout << s[i][j] << endl;
			}
		}*/
		flag = 0;
		bfs();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值