nyist 82迷宫寻宝(一)(BFS)

题目连接:http://acm.nyist.net/JudgeOnline/problem.php?pid=82

此题在基础BFS上加入了门和钥匙,要找齐所有钥匙才能开门,所以要对门特殊处理。

1.先统计下各类钥匙的数量,再记下门的位置。

2.进行BFS,遇到钥匙时,入队,计数各类已找到的钥匙,然后判断是否钥匙找齐,如果找齐,找到对应的门,如果门之前是否访问过,则门入队。(可能此门被障碍堵住,无法进去,所以不能直接加入,只能以前访问过的加入)。

3.如果遇到门,判断钥匙是否找齐,若齐,入队,标记已访问;不齐,不入队,标记已访问。

4.其他的按正常的BFS即可。

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
struct point
{
	int x,y;
}s,e,men[6];
int n,m;
int h[4][2]={-1,0,1,0,0,1,0,-1};
int yaosum[6],findyao[6],bz[30][30];
char map[30][30];
queue<point> q;
int bfs(point s)
{
	int i; point temp,t;
	while(!q.empty()) q.pop();
	q.push(s); bz[s.x][s.y]=1;
	while(!q.empty())
	{
		temp=q.front(); q.pop();
		if(temp.x==e.x&&temp.y==e.y) return 1;
		for(i=0;i<4;i++)
		{
			t.x=temp.x+h[i][0];  t.y=temp.y+h[i][1];
			if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&map[t.x][t.y]!='X')
			{
				if(map[t.x][t.y]>='A'&&map[t.x][t.y]<='E'&&!bz[t.x][t.y])//是门,看是否能打开 
				{
					if(findyao[map[t.x][t.y]-'A']==yaosum[map[t.x][t.y]-'A'])//钥匙齐,可以打开 
					{
						q.push(t); bz[t.x][t.y]=1; 
					}
					else bz[t.x][t.y]=1; //无法打开,先标记已访问 
				}
				else if(map[t.x][t.y]>='a'&&map[t.x][t.y]<='e'&&!bz[t.x][t.y]) //是钥匙 
				{
					findyao[map[t.x][t.y]-'a']++;
					q.push(t); bz[t.x][t.y]=1;
					//判断钥匙是不是已经找齐,并且对应的门是否已访问过,若钥匙齐,门之前访问过,可以把门入队
					if(findyao[map[t.x][t.y]-'a']==yaosum[map[t.x][t.y]-'a'])
					{
						point  m; m.x=men[map[t.x][t.y]-'a'].x; m.y=men[map[t.x][t.y]-'a'].y;
						if(bz[m.x][m.y])  q.push(m); //门入队 
					}
				}
				else if((map[t.x][t.y]=='.'||map[t.x][t.y]=='G')&&!bz[t.x][t.y])
				{
					q.push(t); bz[t.x][t.y]=1;
				}
			}
		}
	}
	return 0;
}
int main(int argc, char *argv[])
{
	int i,j;
	while(cin>>n>>m&&(n+m))
	{
		memset(yaosum,0,sizeof(yaosum));
		memset(findyao,0,sizeof(findyao));
		memset(bz,0,sizeof(bz));
		for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		{
			cin>>map[i][j];
			if(map[i][j]=='S') {s.x=i;s.y=j;}
			else if(map[i][j]=='G') {e.x=i; e.y=j;}
			else if(map[i][j]>='a'&&map[i][j]<='e')  yaosum[map[i][j]-'a']++;
			else if(map[i][j]>='A'&&map[i][j]<='E')
			{
				men[map[i][j]-'A'].x=i; men[map[i][j]-'A'].y=j;
			}
		}
		if(bfs(s)) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}


以下是51单片机控制的12864液晶显示系统编程实现所需的代码: ``` #include <reg51.h> #include <intrins.h> #define LCD_DB P0 sbit LCD_RS = P2^6; // RS信号 sbit LCD_RW = P2^5; // RW信号 sbit LCD_E = P2^7; // E信号 sbit LCD_CS1 = P2^0; // CS1信号 sbit LCD_CS2 = P2^1; // CS2信号 void delay(unsigned int i) // 延时函数 { while(i--); } void write_com(unsigned char com) // 写命令函数 { LCD_RS = 0; LCD_RW = 0; LCD_DB = com; LCD_E = 1; _nop_(); _nop_(); LCD_E = 0; } void write_data(unsigned char dat) // 写数据函数 { LCD_RS = 1; LCD_RW = 0; LCD_DB = dat; LCD_E = 1; _nop_(); _nop_(); LCD_E = 0; } void init_lcd() // 初始化函数 { write_com(0xc0); // 设置显示模式 write_com(0x3f); // 允许显示 write_com(0x40); // 设置起始地址 write_com(0xb8); // 设置页地址 write_com(0xc0); // 设置列地址 write_com(0x40); // 设置起始地址 write_com(0xb8); // 设置页地址 write_com(0xc0); // 设置列地址 } void display_string(unsigned char x, unsigned char y, unsigned char *s) // 显示字符串函数 { unsigned char i; if(y == 0) // 第一行居中显示“南阳理工学院” { write_com(0xb8 + x); write_com(0x40); } else if(y == 1) // 第二行居中显示“www.nyist.edu.cn” { write_com(0xb8 + x); write_com(0x48); } else if(y == 2) // 第三行居中显示“电子信息工程专业” { write_com(0xb8 + x); write_com(0x50); } else if(y == 3) // 第四行居中显示尚春芳 { write_com(0xb8 + x); write_com(0x58); } for(i = 0; s[i] != '\0'; i++) { write_data(s[i]); } } void main() { init_lcd(); // 初始化LCD display_string(18, 0, "南阳理工学院"); // 显示第一行 display_string(16, 1, "www.nyist.edu.cn"); // 显示第二行 display_string(16, 2, "电子信息工程专业"); // 显示第三行 display_string(22, 3, "尚春芳"); // 显示第四行 while(1); } ``` 以上代码实现了你提出的要求,第一行居中显示“南阳理工学院”,第二行居中显示“www.nyist.edu.cn”,第三行居中显示“电子信息工程专业”,第四行居中显示尚春芳。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值