Submission details
Task:Mission
Sender:Team Purkka
Submission time:2015-10-07 18:37:21 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2ACCEPTED0.10 sdetails
#3ACCEPTED0.05 sdetails
#4ACCEPTED0.10 sdetails
#5ACCEPTED0.05 sdetails
#6ACCEPTED0.09 sdetails
#7ACCEPTED0.05 sdetails
#8ACCEPTED0.09 sdetails
#9ACCEPTED0.06 sdetails
#10ACCEPTED0.05 sdetails
#11ACCEPTED0.05 sdetails
#12ACCEPTED0.10 sdetails
#13ACCEPTED0.05 sdetails
#14ACCEPTED0.10 sdetails
#15ACCEPTED0.05 sdetails
#16ACCEPTED0.10 sdetails
#17ACCEPTED0.04 sdetails
#18ACCEPTED0.10 sdetails
#19ACCEPTED0.05 sdetails
#20ACCEPTED0.10 sdetails
#21ACCEPTED0.05 sdetails
#22ACCEPTED0.10 sdetails
#23ACCEPTED0.05 sdetails
#24ACCEPTED0.10 sdetails
#25ACCEPTED0.06 sdetails

Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

int ans = 999999999;
int n, m, k;
int ay, ax, by, bx;
char c[101][101];
vector<pair<int, int>> koliket;
int lol[9][101][101];
bool b[] = {false, false, false, false, false, false, false, false, false};

// O(k)
inline void brute (const int x, const int y, vector<pair<int, int>> v, const int sum, int keratty) {
	//cout<<x<<" "<<y<<endl;
	if (count(v.begin(), v.end(), make_pair(x, y))) {
		//cout<<"return koska miksi ei"<<endl;
		//cout<<"==========="<<endl;
		return;
	}
	v.push_back(make_pair(x, y));
	
	queue<pair<int, int>> q;
	
	int index = 8;
	if (!(x == ax && y == ay)) {
		for (int i = 0; i < k; i++) {
			if (koliket[i] == make_pair(x, y)) {index = i; break;}
		}
	}
	//cout<<"index "<<index<<" "<<v.size()<<endl;
	if (!b[index]) {
		for (int y = 0; y < n; y++) {
			for (int x = 0; x < m; x++) lol[index][y][x] = 888888888;
		}
		lol[index][y][x] = 0;
		q.push(make_pair(x, y));
		b[index] = true;
	}
	
	while (!q.empty()) {
		pair<int, int> p = q.front();
		q.pop();
		int tx = p.first;
		int ty = p.second;
		
		if (ty > 0) {
			if (c[ty - 1][tx] != '#' && lol[index][ty - 1][tx] - 1 > lol[index][ty][tx]) {
				lol[index][ty - 1][tx] = lol[index][ty][tx] + 1;
				q.push(make_pair(tx, ty - 1));
			}
		}
		if (tx > 0) {
			if (c[ty][tx - 1] != '#' && lol[index][ty][tx-1] - 1 > lol[index][ty][tx]) {
				lol[index][ty][tx-1] = lol[index][ty][tx] + 1;
				q.push(make_pair(tx-1, ty));
			}
		}
		if (ty < n - 1) {
			if (c[ty + 1][tx] != '#' && lol[index][ty + 1][tx] - 1 > lol[index][ty][tx]) {
				lol[index][ty + 1][tx] = lol[index][ty][tx] + 1;
				q.push(make_pair(tx, ty + 1));
			}
		}
		if (tx < m - 1) {
			if (c[ty][tx + 1] != '#' && lol[index][ty][tx + 1] - 1 > lol[index][ty][tx]) {
				lol[index][ty][tx + 1] = lol[index][ty][tx] + 1;
				q.push(make_pair(tx + 1, ty));
			}
		}
	}
	/*for (int y = 0; y < n; y++) {
		for (int x = 0; x < m; x++) {
			cout<<lol[index][y][x]<<" ";
		}
		cout<<endl;
	}*/
	//cout<<"==========="<<endl;
	
	if (keratty == k) {
		ans = min(ans, sum + lol[index][by][bx]);
		return;
	} else {
		for (pair<int, int> p : koliket) {
			brute(p.first, p.second, v, sum + lol[index][p.second][p.first], keratty + 1);
		}
	}
}

int main()
{
	cin.sync_with_stdio(false);	

	cin>>n>>m>>k;
	
	for (int y = 0; y < n; y++) {
		for (int x = 0; x < m;x++) {
			cin>>c[y][x];
			if (c[y][x] == 'A') {
				ay = y;
				ax = x;
				c[y][x] = '.';
			} else if (c[y][x] == 'B') {
				by = y;
				bx = x;
				c[y][x] = '.';
			} else if (c[y][x] == '*') {
				koliket.push_back(make_pair(x, y));
			}
		}
	}
	
	vector<pair<int, int>> v;
	brute(ax, ay, v, 0, 0);
	
	cout<<ans<<endl;
	
	return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
84 87 2
##############################...

correct output
104

user output
104

Test 2

Verdict: ACCEPTED

input
100 100 8
##############################...

correct output
264

user output
264

Test 3

Verdict: ACCEPTED

input
26 51 1
##############################...

correct output
48

user output
48

Test 4

Verdict: ACCEPTED

input
100 100 8
##############################...

correct output
145

user output
145

Test 5

Verdict: ACCEPTED

input
68 50 7
##############################...

correct output
88

user output
88

Test 6

Verdict: ACCEPTED

input
100 100 8
##############################...

correct output
187

user output
187

Test 7

Verdict: ACCEPTED

input
61 79 6
##############################...

correct output
149

user output
149

Test 8

Verdict: ACCEPTED

input
100 100 8
##############################...

correct output
232

user output
232

Test 9

Verdict: ACCEPTED

input
94 27 6
###########################
###########################
###########################
#############.#############
...

correct output
82

user output
82

Test 10

Verdict: ACCEPTED

input
100 100 0
##############################...

correct output
30

user output
30

Test 11

Verdict: ACCEPTED

input
65 100 6
######.#######################...

correct output
233

user output
233

Test 12

Verdict: ACCEPTED

input
100 100 8
##############################...

correct output
209

user output
209

Test 13

Verdict: ACCEPTED

input
75 18 3
##################
##################
##################
##################
...

correct output
72

user output
72

Test 14

Verdict: ACCEPTED

input
100 100 8
##############################...

correct output
196

user output
196

Test 15

Verdict: ACCEPTED

input
62 4 3
###.
####
####
####
...

correct output
44

user output
44

Test 16

Verdict: ACCEPTED

input
100 100 8
##############################...

correct output
231

user output
231

Test 17

Verdict: ACCEPTED

input
15 4 0
###.
.###
####
#.#.
...

correct output
4

user output
4

Test 18

Verdict: ACCEPTED

input
100 100 8
##############################...

correct output
300

user output
300

Test 19

Verdict: ACCEPTED

input
86 15 2
.########....##
#########......
#########......
#########......
...

correct output
88

user output
88

Test 20

Verdict: ACCEPTED

input
100 100 8
##############################...

correct output
288

user output
288

Test 21

Verdict: ACCEPTED

input
51 90 3
.................................

correct output
103

user output
103

Test 22

Verdict: ACCEPTED

input
100 100 8
.................................

correct output
352

user output
352

Test 23

Verdict: ACCEPTED

input
22 91 4
.................................

correct output
128

user output
128

Test 24

Verdict: ACCEPTED

input
100 100 8
.................................

correct output
317

user output
317

Test 25

Verdict: ACCEPTED

input
49 42 6
.................................

correct output
175

user output
175