題目大意
給定N*M個方格狀的街區,其中包括:
- 若干建築物,雙向直線的道路以及交叉路口組成了TOC。
- 每個建築物:佔恰好的一個街區。
- 每條道路:所有道路的寬度都爲1個街區,並且道路都是水平的或豎直的。
- 每個交叉路口:佔一個街區,位於道路的交匯處。
保證沒有道路和交叉路口是相同的。
給定通過每個街區的時間,給定每個交叉路口的代號(a-z)(保證沒有重複)。給定起點和終點,還有路徑上所有交叉路口的代號,求在k時刻所在的位置。
解答
這道題可以純模擬來做,記下每一個交叉路口的位置,由於路是直的,所以相鄰兩個經過的交叉路口之間應該有一個座標是相同的,所以我們可以找到路徑上相鄰兩個交叉路口,暴力從起點走到終點,即可得到答案
參考代碼
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int n, m, k;
char ma[200][200];
int x[30], y[30];
int rs, cs, re, ce;
char lu[20000];
int le;
int main()
{
ios::sync_with_stdio(false);
cin >> m >> n >> k;
for (int i = 1; i <= m; i++)
cin >> (ma[i]+1);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++) {
if (ma[i][j] >= 'a' && ma[i][j] <= 'z') {
x[ma[i][j]-'a'] = i;
y[ma[i][j]-'a'] = j;
ma[i][j] = '1';
}
ma[i][j] -= '0';
}
cin >> rs >> cs >> lu >> re >> ce;
int nx = rs, ny = cs;
int tt = 0;
le = strlen(lu);
int fx;
while (tt < le) {
if (x[lu[tt]-'a'] != nx) {
fx = (x[lu[tt]-'a'] > nx) ? 1 : -1;
while (k >= ma[nx][ny] && nx != x[lu[tt]-'a']) {
k -= ma[nx][ny];
nx += fx;
}
if (k < ma[nx][ny]) {
cout << nx << " " << ny;
return 0;
}
tt++;
continue;
}
if (y[lu[tt]-'a'] != ny) {
fx = (y[lu[tt]-'a'] > ny) ? 1 : -1;
while (k >= ma[nx][ny] && ny != y[lu[tt]-'a']) {
k -= ma[nx][ny];
ny += fx;
}
if (k < ma[nx][ny]) {
cout << nx << " " << ny;
return 0;
}
tt++;
continue;
}
}
if (re != nx) {
fx = (re > nx) ? 1 : -1;
while (k >= ma[nx][ny] && nx != re) {
k -= ma[nx][ny];
nx += fx;
}
if (k < ma[nx][ny]) {
cout << nx << " " << ny;
return 0;
}
}
if (ce != ny) {
fx = (ce > ny) ? 1 : -1;
while (k > ma[nx][ny] && ny != ce) {
k -= ma[nx][ny];
ny += fx;
}
if (k < ma[nx][ny]) {
cout << nx << " " << ny;
return 0;
}
}
cout << re << ce;
return 0;
}