ACM Curling 2.0
在行星MM-21上,今年奥运会之后,冰壶(curling)越来越受欢迎。 但规则与我们有所不同。 该游戏是在冰盘上进行的,在冰棋盘上标有方形网格。他们只用一块石头。 游戏的目的是以最少的动作( the minimum number of moves.)让石头从开始到目标位置。
Fig. 1 是游戏板的示例。 一些方块可能被石块占据。 有两个特殊的方格即开始和目标,没有被块占据。 (这两个方块是不同的。)这两个方块是不同的。)一旦石头开始移动,它将继续进行,直到它撞到一个石块。为了把石头带到目标位置,你可能必须通过击中石块来阻止石块,并重新抛出。
Fig. 1: Example of board (S: start, G: goal)
石头的运动遵从以下规则:
- 一开始,石头会在起始方块。
- 石头的运动限于x和y方向。 对角线移动是禁止的。
- 当石头静止时,你可以通过扔它移动。 除非立即被阻止,否则您可以将其扔到任何方向(图2(a))。
- 一旦投掷,石头会继续向同一方向移动,直到发生以下情况之一:
- 石头撞上一块(图2(b),(c))。
- 石头停在撞击它的石块的旁边的正方形上。.
- 石块消失
- 石块从木板上掉出来
- 游戏结束
- 石头到达目标位置
- 石头停在那里,游戏结束了。
- 你不能在比赛中扔石头10次以上。 如果石头在10次移动中没有达到目标,游戏就会失败。
Fig. 2: Stone movements
根据规则(under the rules),我们想知道一开始的石头是否可以达到目标,如果是,则需要最少的移动次数。
在图1所示的初始配置中,需要4次移动才能将石头从开始位置移动到目标位置。 路线如图3(a)所示。 注意当石头到达目标时,游戏板配置如图3(b)所示。
Fig. 3: The solution for Fig. D-1 and the final board configuration
Input
输入是一系列数据集。 输入的结尾由包含由空格分隔的两个零的行指示。 数据集的数量从不超过100。
每个数据集格式如下。
the width(=w) and the height(=h) of the board
First row of the board
...
h-th row of the board
板的宽度和高度满足:2 < = w < = 20,1 < = h < = 20。
每一行都由一个空格分隔的w十进制数组成。这个数字描述了相应的正方形的状态。
0 vacant square 1 block 2 start position 3 goal position
The dataset for Fig. D-1 is as follows:
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
Output
对于每个数据集,打印一行有一个十进制整数,表示从开始到目标的路线的最小移动数。如果没有这样的路由,则输出- 1。每行不应该有除这个数字以外的任何字符。
Sample Input
2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0
Sample Output
1
4
-1
4
10
-1
#include<iostream>
#include<cstdio>
using namespace std;
const int MAX_N = ;
const int INF = <<;
int board[MAX_N][MAX_N];
/*四个方向*/
int dx[] = {,-,,};
int dy[] = {,,,-};
int w,h,bx,by,ex,ey,mintimes;
void solve(int row ,int col,int count)
{
if(count == && board[row][col] != ) //移动次数超10次并且没有到达终点
return ;
else if(board[row][col] == ) //到达终点位置
{
mintimes = min(mintimes,count); //找最小的移动次数
return;
}
for(int i = ; i < ; i++)
{
int ny = row + dy[i];
int nx = col + dx[i];
if(nx >= && ny >= && nx < w && ny < h) //确定冰壶在网格内
{
if(board[ny][nx] == ) continue; //遇到石块
while(nx >= && ny >= && nx < w && ny < h && board[ny][nx] != &&board[ny][nx] != )
{
ny += dy[i];
nx += dx[i];
}
if(nx < || nx >= w || ny < || ny >= h) continue; //飞出冰盘
int temp = board[ny][nx];
int x = nx,y = ny;
if(board[ny][nx] == )
{
board[ny][nx] = ;
ny -= dy[i];
nx -= dx[i];
}
solve(ny,nx,count+);
board[y][x] = temp; //回复原来状态 }
}
}
int main()
{
while(cin>>w>>h,w||h)
{
for(int i =; i < h;i++)
for(int j = ; j < w;j++)
{
scanf("%d",&board[i][j]);
/*标记开始位置和结束位置*/
if(board[i][j] == )
{
bx = j;
by = i;
}else if(board[i][j] == ){
ex = j;
ey = i;
}
}
mintimes = INF;
solve(by,bx,);
if(mintimes == INF)
cout<<"-1"<<endl;
else
cout<<mintimes<<endl; }
return ;
}
ACM Curling 2.0的更多相关文章
- Curling 2.0 分类: 搜索 2015-08-09 11:14 3人阅读 评论(0) 收藏
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14289 Accepted: 5962 Descript ...
- POJ3009——Curling 2.0(DFS)
Curling 2.0 DescriptionOn Planet MM-21, after their Olympic games this year, curling is getting popu ...
- poj 3009 Curling 2.0 (dfs )
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11879 Accepted: 5028 Desc ...
- 【POJ】3009 Curling 2.0 ——DFS
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11432 Accepted: 4831 Desc ...
- Curling 2.0(dfs回溯)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15567 Accepted: 6434 Desc ...
- ****Curling 2.0(深搜+回溯)
Curling 2.0 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- POJ 3009:Curling 2.0 推箱子
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14090 Accepted: 5887 Desc ...
- POJ-3009 Curling 2.0 (DFS)
Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But th ...
- poj3009 Curling 2.0 (DFS按直线算步骤)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14563 Accepted: 6080 Desc ...
随机推荐
- TreeMap就这么简单【源码剖析】
前言 声明,本文用得是jdk1.8 前面章节回顾: Collection总览 List集合就这么简单[源码剖析] Map集合.散列表.红黑树介绍 HashMap就是这么简单[源码剖析] LinkedH ...
- 编写CGI程序步骤
CGI common gateway interface 可以让一个客户端,从网页浏览器向服务器请求数据, 这是描述客户端和服务器程序之间传输数据的一种标准. CGI是运行在服务器上的程序,提供同客户 ...
- 用js来实现那些数据结构(栈01)
其实说到底,在js中栈更像是一种变种的数组,只是没有数组那么多的方法,也没有数组那么灵活.但是栈和队列这两种数据结构比数组更加的高效和可控.而在js中要想模拟栈,依据的主要形式也是数组. 从这篇文章开 ...
- JAVA如何实现深拷贝
protected 域(或方法)微妙的规则 protected 域(或方法)对本包内的所有类可见(当然包括子类),那么,子类可以获得访超类受保护域(或方法)的权利,但是,若子类和超类不在同一个包下,就 ...
- JAVA截取字符串的几种方式
在java中提供了很多字符串截取的方式.下面就来看看大致有几种. 1.split()+正则表达式来进行截取. 将正则传入split().返回的是一个字符串数组类型.不过通过这种方式截取会有很大的性能损 ...
- 计算机网络-TCP之三次握手/四次握手
.概念 .特点 .背景知识补充 .三次握手 .四次握手 .其他补充 1.概念 TCP(Transmission Control Protocol,传输控制协议)是 在不可靠的IP层之上实现的可靠的数据 ...
- Plupload 上传控件使用指南
本文转载至(感谢原作者分享):http://www.cnblogs.com/2050/p/3913184.html#plupload_doc2 我之前写过一篇文章<文件上传利器SWFUpload ...
- [LeetCode] Remove Boxes 移除盒子
Given several boxes with different colors represented by different positive numbers. You may experie ...
- 洛谷P2388 阶乘之乘
题目背景 不告诉你-- 题目描述 求出1!*2!*3!*4!*--*n!的末尾有几个零 输入输出格式 输入格式: n(n<=10^8) 输出格式: 有几个零 输入输出样例 输入样例#1: 复制 ...
- C++Primer学习——各种运算符
前缀递增和后缀递增 class NewInt { public: NewInt():RootInt(0){}; NewInt(int IniInt):RootInt(IniInt){}; NewInt ...