Curling 2.0 POJ 3009(搜索)

原题

题目链接

题目分析

注意题目给的条件,石头到终点会终止.石头碰到冰块会停止,冰块消失.石头滑出边界,游戏失败.石头必须在10回合内到达终点否则游戏失败.按照这个条件写个dfs,记录一下深度就行了.

代码

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <utility>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <cstring>
 7 #include <string>
 8 #include <vector>
 9 #include <stack>
10 #include <queue>
11 #include <map>
12 #include <set>
13
14 using namespace std;
15 const int INF=0x3f3f3f3f;
16
17 int n,m;
18 int mapp[20][20];
19 int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
20 int ans;
21
22 void dfs(int x,int y,int deep)
23 {
24 //    printf("now x=%d y=%d\n",x,y);
25     if(deep==10) return ;
26     for(int i=0;i<4;i++)
27     {
28         bool fault=false;
29         int nx=x+dx[i],ny=y+dy[i];
30         if(nx>=0&&nx<n&&ny>=0&&ny<m&&mapp[nx][ny]!=1)
31         {
32 //            printf("before nx=%d ny=%d\n",nx,ny);
33             while(mapp[nx][ny]!=1&&mapp[nx][ny]!=3)
34             {
35                 nx+=dx[i],ny+=dy[i];
36                 if(nx<0||nx>=n||ny<0||ny>=m)
37                 {
38                     fault=true;
39                     break;
40                 }
41             }
42             if(fault) continue;
43 //            printf("after nx=%d ny=%d\n",nx,ny);
44             if(mapp[nx][ny]==3)
45             {
46                 if(!ans) ans=deep+1;
47                 else ans=min(ans,deep+1);
48             }
49             else
50             {
51                 mapp[nx][ny]=0;
52                 dfs(nx-dx[i],ny-dy[i],deep+1);
53                 mapp[nx][ny]=1;
54             }
55         }
56     }
57     return ;
58 }
59
60 int main()
61 {
62 //    freopen("black.in","r",stdin);
63 //    freopen("black.out","w",stdout);
64     while(~scanf("%d %d",&m,&n)&&n&&m)
65     {
66         ans=0;
67         int x,y;
68         for(int i=0;i<n;i++)
69         for(int j=0;j<m;j++)
70         {
71             scanf("%d",&mapp[i][j]);
72             if(mapp[i][j]==2) x=i,y=j;
73         }
74         dfs(x,y,0);
75         if(ans) printf("%d\n",ans);
76         else printf("-1\n");
77     }
78     return 0;
79 }

原文地址:https://www.cnblogs.com/VBEL/p/11396594.html

时间: 2024-10-10 18:28:07

Curling 2.0 POJ 3009(搜索)的相关文章

DFS:Curling 2.0(POJ 3009)

  冰壶2.0 题目大意:就是给你一个冰壶和一个地图,地图上有石头,冰壶只能沿着x方向和y方向运动,并且要一直运动直到撞到石头为止,并且沿着此方向撞过来会把挡住的石头撞没,冰壶在停的时候可以扔出去一次,最多只能扔10次,问你冰壶能否到指定点? 这一题原题非常长,需要很细心地读题(比如我一开始的时候就是没看到只能扔10次,导致好几次tle),而且这一题一开始我还做复杂了,一开始的时候用的是dfs+dp去储存最短距离,可是我发现冰壶会撞烂石头,所以储存最短距离是不行的,所以只能直接dfs了 直接df

POJ 1979 POJ 3009 AOJ 0033 AOJ 0118 [搜索类题目][0033贪心模拟]

/** POJ 1979 BFS */ #include <stdio.h> #include <string.h> #include <iostream> #include <queue> using namespace std; const int N = 20 + 5; int mp[N][N]; int sx,sy; int n, m; int vis[3000]; int dirx[] = {0, 1, 0, -1}; int diry[] = {

poj 1129 搜索

Channel Allocation Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description When a radio station is broadcasting over a very large area, repeaters are used to ret

POJ 3009 Curling 2.0

Curling 2.0 Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 300964-bit integer IO format: %lld      Java class name: Main On Planet MM-21, after their Olympic games this year, curling is getting popular. But

poj 3009 Curling 2.0 【DFS】

题意:从2出发,要到达3, 0可以通过,碰到1要停止,并且1处要变成0, 并且从起点开始沿着一个方向要一直前进,直至碰到1(或者3)处才能停止,(就是反射来反射去知道反射经过3).如果反射10次还不能到达3,就输出-1. 策略:深搜. 易错点,方向不容易掌握,并且,出题人把n, m顺序反了. 代码: #include<stdio.h> #include<string.h> int map[25][25]; int ans, n, m; const int dir[4][2] = {

【POJ】3009 Curling 2.0 ——DFS

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11432   Accepted: 4831 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

poj 3009 Curling 2.0 (dfs)

id=3009">链接:poj 3009 题意:在一个冰面网格板上,有空白处(无障碍),和障碍块.有一个小石头,给定其起点和终点.求从起点到终点的最小步数 规则:小石头不能在障碍区运动,一旦从某一方向開始运动,不会改变方向,也不会停止.除非碰到障碍物或到达终点才会停止,这为一步.若碰到障碍物.小石头将停在障碍物的旁边,被碰到的一个障碍物将消失. 输入:1代表障碍物(不可到达),0代表空白区,2,代表起点.3代表终点 输出:若小石头能到达终点,且步数最多为十步,输出最小步数,否则输出-1.

POJ 3009 Curling 2.0 (dfs)

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12700   Accepted: 5343 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

poj 3009 Curling 2.0 (dfs )

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11879   Accepted: 5028 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is