【POJ】3009
来源:互联网 发布:知乎名字由来 编辑:程序博客网 时间:2024/06/06 13:58
http://poj.org/problem?id=3009
扔石头,上下左右四个方向如果某一个方向紧挨着block就不能扔这个方向,否则碰到block停住,block消失,再次四个方向扔。
注意剪枝。
#include <iostream>#include <cstring> using namespace std;const int maxn=35;int dx[4]={0,0,1,-1};int dy[4]={1,-1,0,0};int n,m;int sx,sy;int G[maxn][maxn];int ans;void dfs(int x,int y,int step){ if (step>=10||step>ans){ return; } for (int i=0;i<4;i++){ int xx=x+dx[i]; int yy=y+dy[i]; while (xx>=0&&xx<n&&yy>=0&&yy<m){ if (G[xx][yy]==0){ xx+=dx[i]; yy+=dy[i]; } else if (G[xx][yy]==1){ if (!(xx-dx[i]==x&&yy-dy[i]==y)){ G[xx][yy]=0; dfs(xx-dx[i],yy-dy[i],step+1); G[xx][yy]=1; } xx=-1; } else if (G[xx][yy]==3){ if (step+1<ans){ ans=step+1; } xx=-1; } } }}int main(){ while (cin >> m >> n){ if (m==0&&n==0) break; for (int i=0;i<n;i++){ for (int j=0;j<m;j++){ cin >> G[i][j]; } } for (int i=0;i<n;i++){ for (int j=0;j<m;j++){ if (G[i][j]==2){ sx=i; sy=j; i=n; break; } } } G[sx][sy]=0; ans=11; dfs(sx,sy,0); if (ans>10){ cout << "-1" << endl; } else{ cout << ans << endl; } }}
阅读全文
0 0
- POJ 3009
- poj 3009
- POJ 3009
- POJ 3009
- POJ-3009
- POJ 3009
- POJ 3009
- poj-3009
- poj--3009
- poj 3009
- POJ 3009
- poj-3009
- poj 3009
- POJ 3009
- poj 3009
- 【POJ】3009
- POJ 3009
- poj 3009
- 身在上海的她,该不该继续-坚持-前端开发?
- FreeMarker集成
- AngularJS入门小实例
- SQL Server游标的使用
- linux查询删除文件清理空间.txt
- 【POJ】3009
- ListView自带单选和多选的特技Choice Mode
- 大数据Spark企业级实战版【学习笔记】----RDD:分布式函数式编程
- 【比赛练习ac题】poj2253+hdu2717+poj2387+poj1258【解题报告】
- Tensorflow 笔记
- 配置环境变量
- sql语句去除重复数据
- Red5流服务器搭建(实现在线直播,流媒体视频播放和在线视频会议)
- /dev/console,/dev/null,/dev/tty