-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1261.cpp
More file actions
60 lines (58 loc) · 1.33 KB
/
1261.cpp
File metadata and controls
60 lines (58 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#define INF 10000
using namespace std;
bool d[101][101];
int cost[101][101];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
bool visit[101][101];
int main()
{
priority_queue<pair<int,pair<int,int>>>q;
int row,col,a;
cin>>col>>row;
for(int i=1;i<=row;i++)
for(int j=1;j<=col;j++)
{
scanf("%1d",&a);
d[i][j]=(a==1?true:false);
}
for(int i=1;i<=row;i++)
{
for(int j=1;j<=col;j++)
cout<<d[i][j]<<" ";
cout<<endl;
}
visit[1][1]=true;
q.push({0,{1,1}});
int ans=0;
while(!q.empty())
{
int blockcnt=-q.top().first;
int cur_row=q.top().second.first;
int cur_col=q.top().second.second;
q.pop();
if(cur_row==row && cur_col==col){
ans=blockcnt;
break;
}
else
{
for(int i=0;i<4;i++)
{
int n_r=cur_row+dx[i];
int n_c=cur_col+dy[i];
if(n_r>=1&&n_r<=row&&n_c>=1&&n_c<=col&&visit[n_r][n_c]==false)
{
q.push({-((d[n_r][n_c]?1:0)+blockcnt),{n_r,n_c}});
}
}
}
}
cout<<ans;
}