2017年上海金马五校程序设计竞赛:Problem B : Sailing (广搜)
Description
Handoku is sailing on a lake at the North Pole. The lake can be considered as a two-dimensional square plane containing N × N blocks, which is shown in the form of string containing '*' and '#' on the map.
- : a normal block;
: a block containing pack ice.
Handoku is at (1, 1) initially, and his destination is (N, N). He can only move to one of the four adjacent blocks. Sailing around pack ice is dangerous and stressful, so he needs power to remain vigilant. That means if he moves from a '*' block to a '#' block or moves from a '#' block to any another block, he needs to consume 1 unit power. In other cases, he can enjoy the scene on his boat without consuming any power.
Now Handoku wants to know how many units power will be consumed at least during his sailing on the lake.
Input
There are several test cases (no more than 20).
For each test case, the first line contains a single integer N (3 ≤ N ≤ 50), denoting the size of the lake. For the following N lines, each line contains a N-length string consisting of '*' and '#', denoting the map of the lake.
Output
For each test case, output exactly one line containing an integer denoting the answer of the question above.
Sample Input
3
**#
**#
*#*
3
##*
#*#
###
4
**##
#**#
##**
###*
Sample Output
2
4
0
分析:
题意:给出一个NN的地图,起点(1,1),终点(n,n),每次只可以向四个方向走一个格子,地图只由两个字符组成 ''和 '#'。
从字符 ‘*’ 走到字符'#'需要花费1秒时间,从字符'#'走到其他位置(无论那个位置字符是什么)也要花费1秒时间。其他情况不花费时间。求从(1,1)走到(n,n)花费的最小时间。
解题思路:
原来vis数组刷成0,走过的都标1。为了走回头路,新的代码把vis刷成-1,然后对于走过的点,vis放置走到该点的最小时间,这样走到一个点,如果这个点没有访问过,即对应的vis是-1,则该点一定要进队,然后vis变成到达该点的时间。对于已经当过的点,vis数组里面存放的是它上一次到达这个点的最小时间,现在又到了这个点,如果时间比vis里面的时间短,则更新vis,该点再次入队。
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
char Map[52][52];
int vis[52][52];
int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}}; ///搜索方向上右下左
int n;
struct Node
{
int x;
int y;
int power;///能量
friend bool operator < (Node a,Node b)
{
return a.power>b.power;///能量从小打大
}
};
int bfs()
{
Node now,next;
now.x = 1;
now.y = 1;
now.power = 0;
vis[now.x][now.y] = now.power;
priority_queue<Node>qu;
qu.push(now);
while(!qu.empty())
{
now = qu.top();
qu.pop();
if(now.x == n && now.y == n)///达到终点
return now.power;
for(int i = 0; i < 4; i++)
{
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
next.power = now.power;
if(next.x<1||next.x>n||next.y<1||next.y>n) continue;
if(Map[now.x][now.y]=='*')
{
if(Map[next.x][next.y]=='#')
next.power++;
}
else
{
next.power++;
}
if(vis[next.x][next.y]==-1)///没有走过
{
vis[next.x][next.y] = next.power;///直接赋值就行了
qu.push(next);
}
else
{
if(next.power<vis[next.x][next.y])///以前走过,但是现在的时间花费比较小
{
vis[next.x][next.y] = next.power;///更改时间花费
qu.push(next);
}
}
}
}
return -1;
}
int main()
{
while(~scanf("%d",&n))
{
getchar();
///输入地图
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
{
scanf(" %c",&Map[i][j]);
}
memset(vis,-1,sizeof(vis));
int ans = bfs();
printf("%d\n",ans);
}
return 0;
}
2017年上海金马五校程序设计竞赛:Problem B : Sailing (广搜)的更多相关文章
- 2017年上海金马五校程序设计竞赛:Problem K : Treasure Map (蛇形填数)
Description There is a robot, its task is to bury treasures in order on a N × M grids map, and each ...
- 2017年上海金马五校程序设计竞赛:Problem I : Frog's Jumping (找规律)
Description There are n lotus leaves floating like a ring on the lake, which are numbered 0, 1, ..., ...
- 2017年上海金马五校程序设计竞赛:Problem G : One for You (博弈)
Description Given a m × n chessboard, a stone is put on the top-left corner (1, 1). Kevin and Bob ta ...
- 2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome (字符串处理)
Description Given a string S, which consists of lowercase characters, you need to find the longest p ...
- 2017年上海金马五校程序设计竞赛:Problem C : Count the Number (模拟)
Description Given n numbers, your task is to insert '+' or '-' in front of each number to construct ...
- 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)
Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...
- 2017Summmer_上海金马五校 F题,G题,I题,K题,J题
以下题目均自己搜 F题 A序列 一开始真的没懂题目什么意思,还以为是要连续的子串,结果发现时序列,简直智障,知道题意之后,好久没搞LIS,有点忘了,复习一波以后,直接双向LIS,处理处两个数组L和R ...
- HDU 5923 Prediction(2016 CCPC东北地区大学生程序设计竞赛 Problem B,并查集)
题目链接 2016 CCPC东北地区大学生程序设计竞赛 B题 题意 给定一个无向图和一棵树,树上的每个结点对应无向图中的一条边,现在给出$q$个询问, 每次选定树中的一个点集,然后真正被选上的是这 ...
- “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp,K,二分+排序,L,矩阵快速幂,M,线段树区间更新+Lazy思想,N,超级快速幂+扩展欧里几德,O,BFS】
黑白图像直方图 发布时间: 2017年7月9日 18:30 最后更新: 2017年7月10日 21:08 时间限制: 1000ms 内存限制: 128M 描述 在一个矩形的灰度图像上,每个 ...
随机推荐
- Go中的系统Signal处理
package main import "fmt" import "os" import "os/signal" import " ...
- [leetcode-609-Find Duplicate File in System]
https://discuss.leetcode.com/topic/91430/c-clean-solution-answers-to-follow-upGiven a list of direct ...
- restFul介绍及其使用规范
什么是REST和RESTful API? REST:(英文:Representational State Transfer,简称REST)表征性状态转移,是一种软件架构风格. RESTful : RE ...
- STL应用——UVA673(堆栈)
分析:栈的应用,遇到右括号便弹出栈顶元素,看是否与右括号相互匹配,其余情况压入栈. 注意:本题有坑,空串空串,为此我跪了数次 #include<iostream> #include< ...
- 软工实践 - 第二十八次作业 Beta 冲刺(6/7)
队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10146478.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过 ...
- windows下eclipse连接ubuntu伪分布式hadoop2.6.0
环境: win10 jdk1.7 hadoop2.6.0 linux虚拟机 Ubuntu14.04 首先把安装在Ubuntu上的hadoop2.6.0.tar.gz复制到windows系统上,解压到任 ...
- Android Studio的初体验
在机缘巧合之下遇到了安卓开发,接触了Android Studio开始了漫长的改bug的道路,以下为简易版心酸历程 首先我需要成功安装Android Studio,由于我过于叛逆以及为了避免出错于是从一 ...
- Xampp+Openfire+Spark的简单使用
Openfire与Spark的简单实用 1.安装Openfire 百度云 提取码:uu11 2.查找路径 /usr/local/openfire 这时候需要将openfire的文件属性都设置为 可读可 ...
- java线程(4)——线程同步的锁技术
同步 同步,字面来看,有点一起工作的意思.但在线程同步中,"同"意为协同.互相配合. 比如: A.B两个线程,并不是说两个线程必须同时一起工作,而是说互相配合工作,在某个时间可能线 ...
- 数组中键key相等时,后面的值覆盖前面的值
<?php $arr[]='abc'; $arr[]='; $arr[]='; $arr[]='; var_dump($arr); 结果;