思路
按照斜率每次清除整条路上的骷髅(暴力算法),存图选用 (vector <string>),考场上直接开超大的 (char) (MLE) 了。
代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
vector <string > c;
string s;
int n ,m ,x ,y ,ans = 0;
void yuefen (int &r1 ,int &r2) {
int r = __gcd (abs (r1) ,abs (r2));
r1 /= r ,r2 /= r;
}
int main () {
ios_base::sync_with_stdio (0);
c.clear();
cin >> n >> m;
for (int q = 0;q < n;++ q) {
cin >> s;
c.push_back (s);
for (int w = 0;w < m;++ w) {
if (s[w] == 'S')
x = q ,y = w;
}
}
for (int q = 0;q < n;++ q) {
for (int w = 0;w < m;++ w) {
if (c[q][w] == 'K') {
ans ++;
int tx ,ty ,ttx ,tty ,xx ,yy;
xx = x ,yy = y;
ttx = q - x ,tty = w - y;
yuefen (ttx ,tty);
tx = ttx ,ty = tty;
xx += tx ,yy += ty;
for ( ;xx >= 0 && xx < n && yy >= 0 && yy < m ;) {
c[xx][yy] = '.';
xx += tx ,yy += ty;
}
}
}
}
cout << ans << endl;
return 0;
}