码迷,mamicode.com
首页 > 其他好文 > 详细

Patrol Robot UVA - 1600

时间:2017-07-26 23:31:46      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:png   type   pre   poi   struct   连续   最好   name   end   

技术分享

技术分享

 

 题意:给一个矩阵,从(1,1)走到(m,n)的最短路,"1"是障碍,不能连续穿过k个障碍。

 

WA了16次,花费了一个上午和半个下午。。。想??

每个点的属性至少是3维,比如每个点带有的已经连续穿过障碍的次数,再把跑过的步长算上,就四维了,所以最好用一个结构体!

AC代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<queue>
 6 using namespace std;
 7 struct point{
 8     int x,y,step,k;
 9     point(int x,int y,int step,int k):x(x),y(y),step(step),k(k){};    
10 };
11 
12 int T,n,m,kk;
13 int map[30][30];
14 bool vis[30][30];
15 
16 int dx[4]={1,0,-1,0};
17 int dy[4]={0,1,0,-1};
18 
19 int BFS()
20 {   memset(vis,false,sizeof(vis));
21 
22     queue<point> q;
23     q.push(point(0,0,0,kk));
24 
25     int x,y;
26     while(q.size()){
27         x=q.front().x,y=q.front().y;
28         if(x==m-1&&y==n-1) return q.front().step;
29         vis[x][y]=true;
30         for(int i=0;i<4;i++){
31             int mx=dx[i]+x,my=dy[i]+y;
32             if(mx<0||mx>=m||my<0||my>=n||vis[mx][my]) continue;
33             if(map[mx][my]==0) q.push(point(mx,my,q.front().step+1,kk));
34             if(map[mx][my]==1&&q.front().k>0) q.push(point(mx,my,q.front().step+1,q.front().k-1));
35         }
36         q.pop();
37     }
38     return -1;
39 }
40 
41 int main()
42 {    while(cin>>T){
43         while(T--){
44             scanf("%d%d%d",&m,&n,&kk);
45             for(int i=0;i<m;i++){
46                 for(int j=0;j<n;j++) cin>>map[i][j];
47             }
48             cout<<BFS()<<endl;
49         }
50     }
51     return 0;
52 }

WA代码:然而并不是很明白为什么会错。。。。。。。。。。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<queue>
 5 using namespace std;
 6 typedef pair<int,int> P;
 7 
 8 const int INF=100000;
 9 
10 int T,n,m,k;
11 bool vis[30][30];
12 int map[30][30],dis[30][30],cnt[30][30];
13 
14 int dx[4]={1,0,-1,0};
15 int dy[4]={0,1,0,-1};
16 
17 void init()
18 {    
19     for(int i=0;i<m;i++){
20         for(int j=0;j<n;j++){
21             cnt[i][j]=0;
22             dis[i][j]=INF;
23             vis[i][j]=false;    
24         }
25     }
26     dis[0][0]=0;
27     vis[0][0]=true;
28 }
29 
30 void BFS(int start,int end)
31 {    
32     queue<P> q;
33     q.push(make_pair(start,end));
34     
35     while(q.size()){
36         P tem=q.front();
37         q.pop();
38         for(int i=0;i<4;i++){
39             int mx=dx[i]+tem.first,my=dy[i]+tem.second;
40             if(mx<0||mx>=m||my<0||my>=n||vis[mx][my]) continue;
41             if(map[mx][my]==1){
42                 if(map[tem.first][tem.second]==1){
43                     if(cnt[tem.first][tem.second]+1>k) continue;
44                     else cnt[mx][my]=cnt[tem.first][tem.second]+1;
45                 }
46                 else{
47                     cnt[mx][my]=1;
48                     if(cnt[mx][my]>k){
49                         cnt[mx][my]=0;
50                         continue;
51                     }
52                 }
53             }
54             else cnt[mx][my]=0;
55             
56             dis[mx][my]=dis[tem.first][tem.second]+1;
57             vis[mx][my]=true;
58             if(mx==m-1&&my==n-1) return;
59             q.push(make_pair(mx,my));
60         } 
61     }
62 }
63 
64 int main()
65 {    while(cin>>T){
66         while(T--){
67             scanf("%d%d",&m,&n);
68             scanf("%d",&k);
69             for(int i=0;i<m;i++){
70                 for(int j=0;j<n;j++) cin>>map[i][j];
71             }
72             
73             init();
74             BFS(0,0);
75             
76             if(dis[m-1][n-1]==INF) cout<<"-1"<<endl;
77             else cout<<dis[m-1][n-1]<<endl;
78         }
79     }
80     return 0;
81 }

 

Patrol Robot UVA - 1600

标签:png   type   pre   poi   struct   连续   最好   name   end   

原文地址:http://www.cnblogs.com/zgglj-com/p/7242370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!