-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHOP.cpp
More file actions
134 lines (131 loc) · 2.8 KB
/
Copy pathSHOP.cpp
File metadata and controls
134 lines (131 loc) · 2.8 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define RFOR(i,a,b) for(int i=a;i>=b;--i)
#define ln "\n"
#define mp make_pair
#define pb push_back
#define pii pair<ll,ll>
#define sz(a) ll(a.size())
#define debug1(x) cout<<x<<endl
#define debug2(x,y) cout<<x<<"-->"<<y<<endl
#define debug3(x,y,z) cout<<x<<"-->"<<y<<"-->"<<z<<endl
typedef long long ll;
typedef long double ld;
map<ll,ll> counter;
char ch[30][30];
int dp[30][30];
ll fastexpo(ll x,ll y,ll m){ll temp=1;while(y>0){if(y&1)temp = ((temp%m)*(x%m))%m;x = ((x%m)*(x%m))%m;y>>=1;}return temp;}
int w,h,starty,startx,endy,endx;
struct node{
int i,j,dist;
};
bool operator>(const node& a, const node& b)
{
return (a.dist > b.dist);
}
void solve(int ix,int jx)
{
FOR(i,1,h+1)FOR(j,1,w+1)dp[i][j]=INT_MAX;
priority_queue<node,std::vector<node>,greater<node> > qq;
node temp,temp1;
dp[ix][jx]=0;
temp.dist=0;
dp[ix+1][jx]=dp[ix-1][jx]=dp[ix][jx+1]=dp[ix][jx-1]=0;
temp.i=ix+1;
temp.j=jx;
temp.dist=0;
qq.push(temp);
temp.i=ix-1;
temp.j=jx;
temp.dist=0;
qq.push(temp);
temp.i=ix;
temp.j=jx+1;
temp.dist=0;
qq.push(temp);
temp.i=ix;
temp.j=jx-1;
temp.dist=0;
qq.push(temp);
int i,j;
while(sz(qq))
{
// cout<<sz(qq)<<endl;
temp=qq.top();
// debug3(temp.i,temp.j,temp.dist);
qq.pop();
if(ch[temp.i][temp.j]=='D')return;
// if(dp[temp.i][temp.j]<temp.dist)continue;
if(ch[temp.i][temp.j]=='X' || temp.i<1 || temp.i>h || temp.j<1 || temp.j>w)continue;
// debug2(ch[i][j],ch[i][j]-'0');
i=temp.i,j=temp.j;
if(ch[i+1][j]!='X' && ch[i+1][j]!='S')
{
if(dp[i+1][j]>temp.dist+(ch[i][j]-'0'))
{
dp[i+1][j]=min(dp[i+1][j],temp.dist+(ch[i][j]-'0'));
temp1.i=i+1;
temp1.j=j;
temp1.dist=dp[i+1][j];
qq.push(temp1);
}
}
if(ch[i-1][j]!='X' && ch[i-1][j]!='S')
{
if(dp[i-1][j]>temp.dist+(ch[i][j]-'0'))
{
dp[i-1][j]=min(dp[i-1][j],temp.dist+(ch[i][j]-'0'));
temp1.i=i-1;
temp1.j=j;
temp1.dist=dp[i-1][j];
qq.push(temp1);
}
}
if(ch[i][j+1]!='X' && ch[i][j+1]!='S')
{
if(dp[i][j+1]>temp.dist+(ch[i][j]-'0'))
{
dp[i][j+1]=min(dp[i][j+1],temp.dist+(ch[i][j]-'0'));
temp1.i=i;
temp1.j=j+1;
temp1.dist=dp[i][j+1];
qq.push(temp1);
}
}
if(ch[i][j-1]!='X' && ch[i][j-1]!='S')
{
if(dp[i][j-1]>temp.dist+(ch[i][j]-'0'))
{
dp[i][j-1]=min(dp[i][j-1],temp.dist+(ch[i][j]-'0'));
temp1.i=i;
temp1.j=j-1;
temp1.dist=dp[i][j-1];
qq.push(temp1);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
while(1)
{
cin>>w>>h;
if(w==h && h==0)return 0;
FOR(i,1,h+1)
{
FOR(j,1,w+1)
{
cin>>ch[i][j];
if(ch[i][j]=='S'){starty=i;startx=j;}
if(ch[i][j]=='D'){endx=i;endy=j;}
}
}
solve(starty,startx);
cout<<dp[endx][endy]<<ln;
}
return 0;
}