#include<bits/stdc++.h>
using namespace std;
const int N = 30 + 10;
int t, n, m, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}, px[N], py[N];
char c[N][N];
bool mark[N][N], vis[N][N];
double st;
__int128 po[N], base[N], ans, now;
string s;
inline int bfs(int x, int y){
int l = 1, r = 0;
px[++r] = x, py[r] = y, vis[x][y] = true;
while(l <= r){
int stepx = px[l], stepy = py[l ++];
for(int i = 0;i < 4;i = -~i){
int tx = stepx + dx[i], ty = stepy + dy[i];
if(tx < 1 || ty < 1 || tx > n || ty > m || mark[tx][ty] || vis[tx][ty] || c[tx][ty] == '#') continue;
px[++r] = tx, py[r] = ty, vis[tx][ty] = true;
}
}
for(int i = 1;i <= r;i = -~i) vis[px[i]][py[i]] = false;
return r - 1;
}
inline void dfs(int x, int y, int z){
if(now * po[z] + base[z] <= ans) return ;
ans = max(ans, now);
for(int i = 0;i < 4;i = -~i){
int tx = x + dx[i], ty = y + dy[i];
if(tx < 1 || ty < 1 || tx > n || ty > m || mark[tx][ty] || c[tx][ty] == '#') continue;
mark[tx][ty] = true, now = now * 10 + (c[tx][ty] - '0');
dfs(tx, ty, z - 1), mark[tx][ty] = false, now /= 10;
}
return ;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
__int128 step = 1;
for(int i = 0;i < 30;i = -~i) po[i] = step, step *= 10, base[i] = (i == 0 ? 0 : base[i - 1] * 10 + 9);
while(cin>>n>>m){
if(n == 0 && m == 0) break;
st = clock() + 0.03 * CLOCKS_PER_SEC, s = "";
for(int i = 1;i <= n;i = -~i)
for(int j = 1;j <= m;j = -~j)
cin>>c[i][j];
for(int i = 1;i <= n;i = -~i){
for(int j = 1;j <= m;j = -~j){
if(c[i][j] == '#') continue;
mark[i][j] = true, now = (c[i][j] - '0');
dfs(i, j, bfs(i, j)), mark[i][j] = false;
}
}
while(ans) s += (ans % 10 + '0'), ans /= 10;
reverse(s.begin(), s.end()), cout<<s<<'\n';
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N = 30 + 10;
int t, n, m, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}, px[N], py[N];
char c[N][N];
bool mark[N][N], vis[N][N];
double st;
__int128 po[N], base[N], ans, now;
string s;
inline int bfs(int x, int y){
int l = 1, r = 0;
px[++r] = x, py[r] = y, vis[x][y] = true;
while(l <= r){
int stepx = px[l], stepy = py[l ++];
for(int i = 0;i < 4;i = -~i){
int tx = stepx + dx[i], ty = stepy + dy[i];
if(tx < 1 || ty < 1 || tx > n || ty > m || mark[tx][ty] || vis[tx][ty] || c[tx][ty] == '#') continue;
px[++r] = tx, py[r] = ty, vis[tx][ty] = true;
}
}
for(int i = 1;i <= r;i = -~i) vis[px[i]][py[i]] = false;
return r - 1;
}
inline void dfs(int x, int y, int z){
if(clock() >= st || now * po[z] + base[z] <= ans) return ;
ans = max(ans, now);
for(int i = 0;i < 4;i = -~i){
int tx = x + dx[i], ty = y + dy[i];
if(tx < 1 || ty < 1 || tx > n || ty > m || mark[tx][ty] || c[tx][ty] == '#') continue;
mark[tx][ty] = true, now = now * 10 + (c[tx][ty] - '0');
dfs(tx, ty, z - 1), mark[tx][ty] = false, now /= 10;
}
return ;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
__int128 step = 1;
for(int i = 0;i < 30;i = -~i) po[i] = step, step *= 10, base[i] = (i == 0 ? 0 : base[i - 1] * 10 + 9);
while(cin>>n>>m){
if(n == 0 && m == 0) break;
st = clock() + 0.03 * CLOCKS_PER_SEC, s = "";
for(int i = 1;i <= n;i = -~i)
for(int j = 1;j <= m;j = -~j)
cin>>c[i][j];
for(int i = 1;i <= n;i = -~i){
for(int j = 1;j <= m;j = -~j){
if(c[i][j] == '#') continue;
mark[i][j] = true, now = (c[i][j] - '0');
dfs(i, j, bfs(i, j)), mark[i][j] = false;
}
}
while(ans) s += (ans % 10 + '0'), ans /= 10;
reverse(s.begin(), s.end()), cout<<s<<'\n';
}
return 0;
}
为什么第一份代码可以 AC(500ms) ,第二份代码加了卡时,变成 TLE(1200ms) 。
注:唯一的区别是在第二十五行加了卡时。