hdu 1072
来源:互联网 发布:如何在C中调用python 编辑:程序博客网 时间:2024/06/07 04:07
因为0<=n,m<=8的,所以广搜是没有问题的,题目规定每个点可以重复走,只需要将数值为4的点标记就可以了.
#include <iostream>#include <cstdio>#include <queue>#include <cstring>const int maxn = 10;using namespace std;struct node{ int x,y; int time; int ans;};int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};int map[maxn][maxn];int vis[maxn][maxn];int x1,y1;int row,col;int dfs(){ queue<node> que; node t; memset(vis,false,sizeof(vis)); t.time = 6; t.ans = 0; t.x = x1 , t.y = y1; que.push(t); while(!que.empty()){ t = que.front(); que.pop(); node temp = t; if(map[temp.x][temp.y] == 3 && temp.time > 0) return temp.ans; for(int i = 0; i < 4; ++i){ temp.x = t.x + dir[i][0]; temp.y = t.y + dir[i][1]; temp.time = t.time - 1; if(temp.x>0 && temp.x<=row && temp.y>0 && temp.y<=col && map[temp.x][temp.y] && !vis[temp.x][temp.y] && temp.time){ temp.ans = t.ans + 1; if(map[temp.x][temp.y] == 4 ){ temp.time = 6; vis[temp.x][temp.y] = true; } que.push(temp); } } } return -1;}int main(){ int cas; scanf("%d",&cas); while(cas--){ scanf("%d %d",&row,&col); for(int i = 1; i <= row; ++i) for(int j = 1; j <= col; ++j){ scanf("%d",&map[i][j]); if(map[i][j] == 2) x1 = i , y1 = j; } int ans; if((ans = dfs()) != -1) printf("%d\n",ans); else printf("-1\n"); } return 0;}
- hdu 1072
- hdu 1072
- hdu 1072
- hdu 1072
- hdu 1072
- hdu 1072
- hdu-1072
- hdu-1072
- hdu 1072
- HDU 1072
- HDU 1072
- HDU 1072
- DFS基础 (HDU 1010、HDU 1016、HDU 1072)
- HDU 1728 逃离迷宫 + HDU 1072 Nightmare
- HDU 1728 逃离迷宫 + HDU 1072 Nightmare
- HDU 1072(BFS)
- HDU 1072 Nightmare
- BFS hdu 1072 Nightmare
- JSON数据解析
- 天乙社区社区首页分析(三)
- 加载listview配合适配器的使用
- 飞鸽原创博客,真正的飞鸽官方博客
- JDBC介绍
- hdu 1072
- linux命令--sysctl
- linux-3.4.2 s5pv210 LCD驱动移植 overlay(二)
- jquery 基础语法
- js学习
- Oracle中的几个概念——db_name db_domain instance SID
- Android异步下载网络图片(其一)
- Java调用XML的方法之DocumentBuilderFactory
- ios的@property属性和@synthesize属性