-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectingRiders.cpp
More file actions
85 lines (80 loc) · 2.14 KB
/
Copy pathCollectingRiders.cpp
File metadata and controls
85 lines (80 loc) · 2.14 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
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int arrx[8]={1, 2, 2, 1, -1, -2, -2, -1};
int arry[8]={2, 1, -1, -2, -2, -1, 1, 2};
class CollectingRiders {
public:
int minimalMoves(vector <string> board)
{
int n=board.size();
int m=board[0].size();
int moves=-1;
int dist[11][11];
for(int sx=0;sx<n;sx++)
{
for(int sy=0;sy<m;sy++)
{
cout<<"For "<<sx<<" "<<sy<<endl;
//for(int i=0;i<n;i++) for(int j=0;j<m;j++) dist[i][j]=10000;
fill(dist[0], dist[10], 10000);
dist[sx][sy]=0;
queue<int>Q;
Q.push(sx); Q.push(sy);
while(!Q.empty())
{
int x=Q.front(); Q.pop();
int y=Q.front(); Q.pop();
int mm=dist[x][y];
for(int i=0;i<8;i++)
{
//cout<<x+arrx[i]<<" "<<y+arry[i]<<" "<<dist[x+arrx[i]][y+arry[i]]<<" "<<n<<" "<<m<<endl;
if(x+arrx[i] >= 0 && x+arrx[i] < n && y+arry[i] >= 0 && y+arry[i] < m && (dist[x+arrx[i]][y+arry[i]] == 10000))
{
//cout<<"inserting " << x+arrx[i]<<" "<<y+arry[i]<<endl;
Q.push(x+arrx[i]);
Q.push(y+arry[i]);
dist[x+arrx[i]][y+arry[i]]=mm+1;
}
}
}
int s=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(board[i][j]!='.')
{
if(dist[i][j]==10000)
dist[i][j]=-100000000;
else
s+=dist[i][j]==0 ? 0 : (dist[i][j]-1)/(board[i][j]-'0')+1;
}
}
}
if(s>=0 && (moves<0 || s<moves))
moves=s;
}
}
return moves;
}
};
<%:testing-code%>
//Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!