hdih1026

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Special Judge


Problem Description
The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:

1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.

Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
 

Input
The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.
 

Output
For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.
 

Sample Input
5 6 .XX.1. ..X.2. 2...X. ...XX. XXXXX. 5 6 .XX.1. ..X.2. 2...X. ...XX. XXXXX1 5 6 .XX... ..XX1. 2...X. ...XX. XXXXX.
 

Sample Output
It takes 13 seconds to reach the target position, let me show you the way. 1s:(0,0)->(1,0) 2s:(1,0)->(1,1) 3s:(1,1)->(2,1) 4s:(2,1)->(2,2) 5s:(2,2)->(2,3) 6s:(2,3)->(1,3) 7s:(1,3)->(1,4) 8s:FIGHT AT (1,4) 9s:FIGHT AT (1,4) 10s:(1,4)->(1,5) 11s:(1,5)->(2,5) 12s:(2,5)->(3,5) 13s:(3,5)->(4,5) FINISH It takes 14 seconds to reach the target position, let me show you the way. 1s:(0,0)->(1,0) 2s:(1,0)->(1,1) 3s:(1,1)->(2,1) 4s:(2,1)->(2,2) 5s:(2,2)->(2,3) 6s:(2,3)->(1,3) 7s:(1,3)->(1,4) 8s:FIGHT AT (1,4) 9s:FIGHT AT (1,4) 10s:(1,4)->(1,5) 11s:(1,5)->(2,5) 12s:(2,5)->(3,5) 13s:(3,5)->(4,5) 14s:FIGHT AT (4,5) FINISH God please help our poor hero. FINISH
 

Author
Ignatius.L

#include<stdio.h>
#include<memory.h>
int N,M;
char map0[100][100];
int map[100][100];
int time[100][100];
int phead,ptail,qhead,qtail;
struct queue{
	int x;
	int y;
}queue[50000];
struct path{
	int x;
	int y;
}path[500][500];
int direction[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int inarea(int x,int y)
{
	return (x>=0&&y>=0&&x<N&&y<M&&map[x][y]!=-1);
}
void bfs(void)
{
	qhead=0;
	qtail=0;
	time[0][0]=0;
	queue[qtail].x=0;
	queue[qtail++].y=0;
	while(qhead<qtail){
		for(int i=0;i<4;i++){
			if(inarea(queue[qhead].x+direction[i][0],queue[qhead].y+direction[i][1])&&
				time[queue[qhead].x][queue[qhead].y]+map[queue[qhead].x+direction[i][0]][queue[qhead].y+direction[i][1]]+1
				<time[queue[qhead].x+direction[i][0]][queue[qhead].y+direction[i][1]]){
				time[queue[qhead].x+direction[i][0]][queue[qhead].y+direction[i][1]]=
					time[queue[qhead].x][queue[qhead].y]+map[queue[qhead].x+direction[i][0]][queue[qhead].y+direction[i][1]]+1;
				queue[qtail].x=queue[qhead].x+direction[i][0];
				queue[qtail++].y=queue[qhead].y+direction[i][1];
				path[queue[qhead].x+direction[i][0]][queue[qhead].y+direction[i][1]].x=queue[qhead].x;
				path[queue[qhead].x+direction[i][0]][queue[qhead].y+direction[i][1]].y=queue[qhead].y;
			}
		}
		qhead++;
	}
}
int main(void)
{
	while(scanf("%d %d",&N,&M)!=EOF){
		getchar();
		for(int i=0;i<N;i++){
			for(int j=0;j<M;j++){
				scanf("%c",&map0[i][j]);
				time[i][j]=50000;
				switch(map0[i][j]){
					case'X':
						map[i][j]=-1;
						break;
					case'.':
						map[i][j]=0;
						break;
					default:
						map[i][j]=map0[i][j]-'1'+1;
				}
			}
			getchar();
		}
		phead=0;
		ptail=0;
		bfs();	
		qtail=1;
		int x=N-1;
		int y=M-1;
		int m=x;
		int n=y;  
		queue[qtail].x=x;
		queue[qtail++].y=y;
		while(x!=0||y!=0){
			m=x;
			n=y;
			x=path[m][n].x;
			y=path[m][n].y;
			queue[qtail].x=x;
			queue[qtail++].y=y;
		}
		if(time[N-1][M-1]<50000){
			printf("It takes %d seconds to reach the target position, let me show you the way.\n",time[N-1][M-1]);
			int time=0;
			while(--qtail){
				while(map[queue[qtail].x][queue[qtail].y]--)
					printf("%ds:FIGHT AT (%d,%d)\n",++time,queue[qtail].x,queue[qtail].y);
				if(qtail!=1)
					printf("%ds:(%d,%d)->(%d,%d)\n",++time,queue[qtail].x,queue[qtail].y,queue[qtail-1].x,queue[qtail-1].y);
			}
		}
		else printf("God please help our poor hero.\n");
		printf("FINISH\n");
	}
	return 0;
}


下载方式:https://pan.quark.cn/s/a4b39357ea24 布线问题(分支限界算法)是计算机科学和电子工程领域中一个广为人知的议题,它主要探讨如何在印刷电路板上定位两个节点间最短的连接路径。 在这一议题中,电路板被构建为一个包含 n×m 个方格的矩阵,每个方格能够被界定为可通行或不可通行,其核心任务是定位从初始点到最终点的最短路径。 分支限界算法是处理布线问题的一种常用策略。 该算法与回溯法有相似之处,但存在差异,分支限界法仅需获取满足约束条件的一个最优路径,并按照广度优先或最小成本优先的原则来探索解空间树。 树 T 被构建为子集树或排列树,在探索过程中,每个节点仅被赋予一次成为扩展节点的机会,且会一次性生成其全部子节点。 针对布线问题的解决,队列式分支限界法可以被采用。 从起始位置 a 出发,将其设定为首个扩展节点,并将与该扩展节点相邻且可通行的方格加入至活跃节点队列中,将这些方格标记为 1,即从起始方格 a 到这些方格的距离为 1。 随后,从活跃节点队列中提取队首节点作为下一个扩展节点,并将与当前扩展节点相邻且未标记的方格标记为 2,随后将这些方格存入活跃节点队列。 这一过程将持续进行,直至算法探测到目标方格 b 或活跃节点队列为空。 在实现上述算法时,必须定义一个类 Position 来表征电路板上方格的位置,其成员 row 和 col 分别指示方格所在的行和列。 在方格位置上,布线能够沿右、下、左、上四个方向展开。 这四个方向的移动分别被记为 0、1、2、3。 下述表格中,offset[i].row 和 offset[i].col(i=0,1,2,3)分别提供了沿这四个方向前进 1 步相对于当前方格的相对位移。 在 Java 编程语言中,可以使用二维数组...
源码来自:https://pan.quark.cn/s/a4b39357ea24 在VC++开发过程中,对话框(CDialog)作为典型的用户界面组件,承担着与用户进行信息交互的重要角色。 在VS2008SP1的开发环境中,常常需要满足为对话框配置个性化背景图片的需求,以此来优化用户的操作体验。 本案例将系统性地阐述在CDialog框架下如何达成这一功能。 首先,需要在资源设计工具中构建一个新的对话框资源。 具体操作是在Visual Studio平台中,进入资源视图(Resource View)界面,定位到对话框(Dialog)分支,通过右键选择“插入对话框”(Insert Dialog)选项。 完成对话框内控件的布局设计后,对对话框资源进行保存。 随后,将着手进行背景图片的载入工作。 通常有两种主要的技术路径:1. **运用位图控件(CStatic)**:在对话框界面中嵌入一个CStatic控件,并将其属性设置为BST_OWNERDRAW,从而具备自主控制绘制过程的权限。 在对话框的类定义中,需要重写OnPaint()函数,负责调用图片资源并借助CDC对象将其渲染到对话框表面。 此外,必须合理处理WM_CTLCOLORSTATIC消息,确保背景图片的展示不会受到其他界面元素的干扰。 ```cppvoid CMyDialog::OnPaint(){ CPaintDC dc(this); // 生成设备上下文对象 CBitmap bitmap; bitmap.LoadBitmap(IDC_BITMAP_BACKGROUND); // 获取背景图片资源 CDC memDC; memDC.CreateCompatibleDC(&dc); CBitmap* pOldBitmap = m...
【集群划分】基于kmeans的电压调节的集群划分【IEEE33节点】内容概要:本文围绕基于KMeans算法的电压调节集群划分展开,以IEEE33节点配电网为研究对象,探讨含分布式光伏的配电网中电压协调控制问题。通过KMeans聚类算法将网络节点划分为若干电压调控集群,旨在降低电压越限风险、提升配电网运行稳定性。文中结合Matlab代码实现,详细展示了集群划分过程、聚类结果可视化及后续电压协调控制策略的设计思路,适用于电力系统中分布式能源接入带来的电压管理挑战。该方法有助于实现分区治理、优化资源配置,并为后续的分布式控制提供结构基础。; 适合人群:具备电力系统基础知识,熟悉Matlab编程,从事配电网优化、分布式能源管理或智能电网相关研究的研究生及科研人员;有一定机器学习背景的工程技术人员。; 使用场景及目标:①应用于含高渗透率光伏发电的配电网电压调控研究;②用于复现IEEE33节点系统中的集群划分与电压协调控制模型;③支撑科研论文复现、课题开发与算法验证,推动智能配电网的分区协同控制技术发展; 阅读建议:建议结合提供的Matlab代码进行实践操作,重点关注KMeans在电网拓扑数据上的特征选取与距离度量方式,理解聚类结果对电压控制性能的影响,并可进一步拓展至动态聚类或多目标优化集成。
先看效果: https://pan.quark.cn/s/92cf62472d7f 在C++编程领域中,**流类库与输入输出**构成了极为关键的基础元素,其主要功能在于管理程序与外部设备之间的数据传递。 流类库通过提供一系列丰富的类和函数,为这种数据交互提供了强大的支持,从而让开发人员能够便捷地完成输入输出任务。 ### 三种核心的输出流#### 1. `ostream``ostream`类作为一个输出流的对象,在流类库中扮演着核心的角色。 它通常用于将数据传输至标准输出设备(例如显示屏)。 `cout`作为一个预定义的`ostream`对象,主要用于标准输出。 ##### 特点:- 默认情况下与标准输出设备相连接。 - 能够重新指向其他输出设备,比如文件。 - 支持输出多种类型的数据,涵盖字符串、数字等。 - 提供了多样化的格式化输出选项。 #### 2. `ofstream``ofstream`类作为`ostream`的一个派生类,专门用于执行文件输出操作。 它使得开发人员能够将数据写入到磁盘文件中。 ##### 特点:- 在使用时自动打开文件以进行写入操作。 - 提供了多种文件打开模式,包括追加、覆盖等。 - 支持以二进制和文本两种模式进行输出。 - 能够方便地进行错误状态检测。 #### 3. `ostringstream``ostringstream`类同样是`ostream`的派生类,但它用于在内存中构建字符串流,而不是直接输出到显示屏幕或文件。 这对于需要动态生成字符串的应用场景非常适用。 ##### 特点:- 将输出结果暂存于内存之中。 - 可以转换为常规字符串格式。 - 适用于动态构建字符串序列。 - 常用于日志记录、数据格式化等场景。 ### 流的操作机制流可以被理解为一种“字节传...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值