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 ...
随机推荐
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) A. Trip For Meal
http://codeforces.com/contest/876/problem/A 题意: 一个人一天要吃n次蜂蜜,他有3个朋友,他第一次总是在一个固定的朋友家吃蜂蜜,如果说没有吃到n次,那么他就 ...
- Struts(十八):通过CURD来学习PrepareInterceptor拦截器
PrepareInterceptor拦截器的用法: 1.若Action实现了Preparable接口,则Action方法需实现prepare()方法: 2.PrepareInterceptor拦截器S ...
- ROS系统MoveIt玩转双臂机器人系列(一)
一.ROS系统的MoveIt模块简介 机器人操作系统ROS目前最受关注的两个模块是导航(Navigation)和机械臂控制(MoveIt!),其中,机械臂控制模块(后面简称MoveIt)可以让用户快速 ...
- laydate 日期格式为yyyy 或yyyy-MM时,出现错误Uncaught TypeError: Cannot read property 'length' of undefined
这个改起比较麻烦,没有深究,简单兼容了yyyy 和yyyy-MM,其他格式可能还是会有错误.替换Dates.check方法. //检测日期是否合法 Dates.check = function(){ ...
- java学习历程,一年三年五年计划
学习这一部分其实也算是今天的重点,这一部分用来回答很多群里的朋友所问过的问题,那就是你是如何学习Java的,能不能给点建议?今天我是打算来点干货,因此咱们就不说一些学习方法和技巧了,直接来谈每个阶段要 ...
- [LeetCode] Longest Line of Consecutive One in Matrix 矩阵中最长的连续1
Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...
- 【NOIP2016】愤怒的小鸟
题目描述 Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可以用它向第一象限发射一只红色的小鸟,小鸟们的飞行轨迹均为形 ...
- 51Nod 1555 布丁怪
题目描述: 布丁怪这一款游戏是在一个n×n 的矩形网格中进行的,里面有n个网格有布丁怪,其它的一些格子有一些其它的游戏对象.游戏的过程中是要在网格中移动这些怪物.如果两个怪物碰到了一起,那么他们就会变 ...
- bzoj1791: [Ioi2008]Island 岛屿 单调队列优化dp
1791: [Ioi2008]Island 岛屿 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1826 Solved: 405[Submit][S ...
- QCA4028软件平台启用双WAN指导
1 为何要启用双WAN QCA4028的硬件方案,基板上部署了一个LTE模块插槽,同时又外留了一个USB3.0接口,因此,就可以在此硬件平台上调试基于LTE的双WAN,预期实现: A 链路备份,在任意 ...