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 (广搜)的更多相关文章

  1. 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 ...

  2. 2017年上海金马五校程序设计竞赛:Problem I : Frog's Jumping (找规律)

    Description There are n lotus leaves floating like a ring on the lake, which are numbered 0, 1, ..., ...

  3. 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 ...

  4. 2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome (字符串处理)

    Description Given a string S, which consists of lowercase characters, you need to find the longest p ...

  5. 2017年上海金马五校程序设计竞赛:Problem C : Count the Number (模拟)

    Description Given n numbers, your task is to insert '+' or '-' in front of each number to construct ...

  6. 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)

    Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...

  7. 2017Summmer_上海金马五校 F题,G题,I题,K题,J题

    以下题目均自己搜 F题  A序列 一开始真的没懂题目什么意思,还以为是要连续的子串,结果发现时序列,简直智障,知道题意之后,好久没搞LIS,有点忘了,复习一波以后,直接双向LIS,处理处两个数组L和R ...

  8. HDU 5923 Prediction(2016 CCPC东北地区大学生程序设计竞赛 Problem B,并查集)

    题目链接  2016 CCPC东北地区大学生程序设计竞赛 B题 题意  给定一个无向图和一棵树,树上的每个结点对应无向图中的一条边,现在给出$q$个询问, 每次选定树中的一个点集,然后真正被选上的是这 ...

  9. “盛大游戏杯”第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 描述 在一个矩形的灰度图像上,每个 ...

随机推荐

  1. beego orm

    http://beego.me/docs/mvc/model/overview.md go get github.com/astaxie/beego/orm Simple Usage package ...

  2. Android 序列化比对

    本文转自:https://www.zybuluo.com/linux1s1s/note/91046 注:部分内容有更改 在Android中使用序列化,无非两种途经: Parcelable 和 Seri ...

  3. Prolog奇怪奇妙的思考方式

    今天在<七周七语言>中接触到了prolog,发现它的编程模式和思考方式的确比较奇怪,但同时也非常奇妙,值得学习一下. 1. prolog语言介绍     和SQL一样,Prolog基于数据 ...

  4. asp.net webapi 使用小结

    一星期前公司用webapi处理一些事情,自己总结一下用法. 1.创建一个空的webapi会默认有一下几个方法. public class ValueController : ApiController ...

  5. 签名APK后仍然出现INSTALL_PARSE_FAILED_NO_CERTIFICATES的解决方案

    修改apk里的dex并且修复后重新打包进apk里,使用signapk.jar签名后安装仍然出现INSTALL_PARSE_FAILED_NO_CERTIFICATES,搜了很久,使用了多种方法签名仍然 ...

  6. Python网络编程(socketserver、TFTP云盘、HTTPServer服务器模型)

    HTTP协议? HTTP是一个应用层协议,由请求和响应构成,是一个标准的客户端服务器模型.HTTP是一个无状态的协议. 通常承载于TCP协议之上,有时也承载于TLS或SSL协议层之上,这个时候,就成了 ...

  7. m个苹果放在n个盘子中有多少种结果

    题目 m个苹果放在n个盘子中有多少种结果,前置条件: 允许存在空盘 重复的摆放结果忽略不计 根据题意,也就是有3种情况,的确完全重复的摆放方式是没多大意义的 思路 这题可以用枚举的描述方式进行尾递归求 ...

  8. NO8——排序

    //sort #include<algorithm> bool cmp(const int a,const int b) { return a>b;//降序排列 } //qsort ...

  9. 【翻译】ASP.NET Core 文档目录

    微软官方CORE 2.0正式版中文文档已经出来了,地址:https://docs.microsoft.com/zh-cn/aspnet/core/ 简介 入门 创建一个Web应用程序 创建一个Web ...

  10. 算法(3)Rotate Array

    题目:将一个n个元素的数组右移k位,比如n=7,k=3,对数组[1,2,3,4,5,6,7]作如下旋转[5,6,7,1,2,3,4] 思路:[5,6,7,1,2,3,4],不知大家看出来了没有呢,两次 ...