`
godfrey90
  • 浏览: 54742 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

8th_D

阅读更多
                         D. 穿越机房
问题描述
   众所周知,在机房里想从一个地方到另一个地方很困难。因为机房里不仅有很多的
桌子,还摆了很多的椅子。现在,建冬哥想找嘉娃一起 apom,他不能在众目睽睽之下
对着嘉娃大吼,所以,他必须走到嘉娃跟前。
   我们把机房分为宽度为 w,高度为 h 的 w × h 个格子。每个格子或者是一张桌子,
或者有一把椅子,或者什么都没有。建冬哥不能通过有桌子的格子,可以以一个单位时
间通过什么都没有的格子,但是通过有椅子的格子时,建冬哥需要花两个单位时间。
   现在给出建冬哥和嘉娃的位置,以及机房的构造,你能知道建冬哥最快要多长时间
才能到嘉娃身边吗?
输入格式
   第一行 6 个整数 w, h, sr, sc, er, ec(3 ≤ w, h ≤ 20)。 w 和 h 表示机房的大小, (sr, sc)
表示建冬哥处在第 sr 行第 sc 列,同理 (er, ec) 表示目前嘉娃的位置。开始时建冬哥和
嘉娃肯定在机房里。接下来是一个 w × h 的矩阵,代表机房的地形:
   • '.': 表示该格子没有障碍物,通过需要 1 个单位时间。
   • '*': 表示该格子有一把椅子,通过需要 2 个单位时间。
   • 'x': 表示该格子是一张桌子,不能通过。
输出格式
   一个整数,表示建冬哥到嘉娃所在位置的最短时间。之后加一个换行。
样例输入
4 6 1 1 4 6
******
......
*xx*x.
.*...*
样例输出
11
提示
   最后的输出结果为建冬哥经过路径上所有点的时间之和。
#include<cstdio>

int a[22][22];
int w=0,h=0,sr=0,sc=0,er=0,ec=0;
int min=100000;

void prework(){
	scanf("%d %d %d %d %d %d",&w,&h,&sr,&sc,&er,&ec);
	for(int j=1;j<=h;j++){
		a[0][j]=-1;
		a[w+1][j]=-1;
	}
	for(int i=1;i<=w;i++){
		a[i][0]=-1;
		a[i][h+1]=-1;
	}
	char s[20];
	for(int i=1;i<=w;i++){
		scanf("%s",s);
		for(int j=1;j<=h;j++){
			if(s[j-1]=='.')
				a[i][j]=1;
			else if(s[j-1]=='*')
				a[i][j]=2;
			else if(s[j-1]=='X')
				a[i][j]=-1;
		}
	}
}
void search(){
	if((sr==er)&&(sc==ec)){
		min = a[sr][sc];
		return;
	}
	int del_r[4]={1,-1,0,0};
	int del_c[4]={0,0,1,-1};
	struct unit{
		int r;
		int c;
		int time;
	};
	unit queue[4000];
	int p=0,q=1;


	queue[0].r = sr;
	queue[0].c = sc;
	queue[0].time=a[sr][sc];
//	printf("%d: %d %d %d\n",p,queue[p].r,queue[p].c,queue[p].time);
	while(p!=q){
		for(int i=0;i<4;i++){
			int r = queue[p].r+del_r[i];
			int c = queue[p].c+del_c[i];
			if(1==a[r][c]){
				bool flag = true;
				for(int j=0;j<q;j++){
					if((queue[j].r==r)&&(queue[j].c==c)&&(queue[j].time<=queue[p].time+1)){
						flag = false;
						break;
					}
				}
				if(!flag) continue;
				queue[q].r=r;
				queue[q].c=c;
				queue[q].time = queue[p].time+1;

//				printf("%d: %d %d %d\n",q,queue[q].r,queue[q].c,queue[q].time);
				if((queue[q].r==er)&&(queue[q].c==ec)&&(min>queue[q].time)){
					min = queue[q].time;
				}
				q++;
			}
		}
		for(int i=0;i<4;i++){
			int r = queue[p].r+del_r[i];
			int c = queue[p].c+del_c[i];
			if(2==a[r][c]){
				bool flag = true;
				for(int j=0;j<q;j++){
					if((queue[j].r==r)&&(queue[j].c==c)&&(queue[j].time<=queue[p].time+2)){
						flag = false;
						break;
					}
				}
				if(!flag) continue;
				queue[q].r=r;
				queue[q].c=c;
				queue[q].time = queue[p].time+2;

//				printf("%d: %d %d %d\n",q,queue[q].r,queue[q].c,queue[q].time);
				if((queue[q].r==er)&&(queue[q].c==ec)&&(min>queue[q].time)){
					min = queue[q].time;
				}
				q++;
			}
		}
		p++;
	}
}
int main(){
	prework();
//	for(int i=0;i<=w+1;i++){
//		for(int j=0;j<=h+1;j++){
//			printf("%d ",a[i][j]);
//		}
//		printf("\n");
//	}
	search();
	printf("%d\n",min);
	return 0;
}



分享到:
评论

相关推荐

    Understanding Automotive Electronics 8th - Appendix D

    Understanding Automotive Electronics 8th Appendix D - FDI Feedback Matrix Design

    C++ Programming: From Problem Analysis to Program Design, 8th Edition

    By 作者: D. S. Malik ISBN-10 书号: 1337102083 ISBN-13 书号: 9781337102087 Edition 版本: 8 出版日期: 2017-02-13 pages 页数: 1491 Contents Chapter 1. An Overview Of Computers And Programming Languages...

    OpenGL-Programming-Guide-8th-Edition-Code

    OpenGL-Programming-Guide-8th-Edition-Code这是OpenGL编程指南(第八版)书中代码,使用VS2015建立的工程。目标平台版本是8.1,平台工具集是v140。 使用高版本VS打开项目时,如果不想升级可以安装 ,安装v140平台...

    Statistical Method for Psychology (Howell)

    This seventh edition of Statistical Methods for Psychology, like the previous editions, surveys statistical techniques commonly used in the behavioral and social sciences, especially ...

    Integrated Power Brake (IPB)

    With the introduction of vehicle stability systems like ESP®, and passive safety systems like seat belts and airbags, driving safety has been improved and the number of road traffic ...

    Probability_and_Statistical_Inference:R代码,用于概率论和统计推断

    概率与统计推断本课程介绍了R的概率和统计推断。... Garland,P.-R统计数据,Springer 2008 詹姆斯(James,G.),维滕(Witten),D。 统计学习简介(第112卷,第18页)。 纽约:史宾格。 图书网站:

    Predict whether income exceeds $50K/yr based on census data. Als

    Machine Learning, Classification problem.... Also known as "Census Income" ... education: Bachelors, Some-college, 11th, HS-grad, Prof-school, Assoc-acdm, Assoc-voc, 9th, 7th-8th, 12th, Masters, 1st-4th, 10

    BobBuilder_app

    The road not taken / the road taken and doubled back! Performance Tests Comparing B+tree and MGIndex Really big data sets! Index parameter tuning Performance Tests - v2.3 Using the Code Differences to...

    JavaScript音频性能工具Lissajous.zip

    // load an array of three AudioBuffers called 'drums', play them in 8th notes and give them // the sequence drums[0], drums[2], drums[1], drums[2] d = new track() d.sample...

    Data-Analytics-project-On-Census-Data

    数据分析项目普查数据这是在人口普查数据集... 教育程度:学士,部分大学,11年级,高中毕业生,教授学校,Assoc-acdm,Assoc-voc,9、7th8th,12th,硕士,1至4、10,博士学位,5至6,学前班。 education-num:连续的

    Thinking with Types

    And so I started a quick document, jotting down a bunch of type-level programming topics I thought it’d be fun to write blog posts about. This document rather quickly turned into an outline ofwhat ...

    算法上机!!

    Date: Monday, May 8th, 2013 We highly encourage being environment friendly and trying all problems on your own. 0/1 Knapsack Problem. There are 5 items that have a value and weight list below, the ...

    雷达技术知识

    different levels of the waving d (d = scan angle). Figure shows the relationship of scan angle of LiDAR to return from a water surface. Return factor is greatest at low scan angles relative to the ...

    API程序文件1.doc

    唐山文丰启源管业有限公司 程序文件 (依据TSG D7001-2006、TSG D7002-2006和GB/T 19001:2008标准编写) 文件编号: WFQY/QP-2010 发行版本: A/0 受控状态: 发布实施日期: 2010年1月4日 程序文件目录 "序号 "对应...

    Ebooks For Dummies Collection

    D: Dating For Dummies.pdf Design Patterns For Dummies.pdf Digital Art Photography For Dummies.pdf Digital Photography AIO Desk Reference 3rd Ed For Dummies.pdf Digital Photography AIO Desk Reference ...

Global site tag (gtag.js) - Google Analytics