题目大意
给定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;
}