QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#368244 | #8236. Snake Move | wdw | RE | 0ms | 0kb | C++20 | 3.9kb | 2024-03-26 22:20:36 | 2024-03-26 22:20:37 |
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
const int N = 5e5 + 5;
const int mod = 1e9+7;
const double eps =0.00001;
#define int long long
#define ull unsigned long long
#define endl '\n'
ull mod1=1;
using i64 = int64_t;
int ksm(int a,int b){
int ans=1;
while(b){
if(b%2)ans=ans*a%mod;
a=a*a%mod;
b/=2;
}
return ans;
}
struct node{
int cnt=0,x,y,k;
};
vector<vector<char>>pic;
vector<vector<int>>idx,dis,vis;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void solve() {
int n,m,k;
cin>>n>>m>>k;
pic=vector<vector<char>>(n+5,vector<char>(m+5));
vis=dis=idx=vector<vector<int>>(n+5,vector<int>(m+5));
int x,y;
for(int i=1,x1,y1;i<=k;i++){
if(i==1){
cin>>x>>y;
idx[x][y]=i;
}else{
cin>>x1>>y1;
idx[x1][y1]=i;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>pic[i][j];
dis[i][j]=1e9;
}
}
dis[x][y]=0;
queue<node>q;
q.push({0,x,y,k});
while(!q.empty()){
auto y=q.front();
q.pop();
if(vis[y.x][y.y])continue;
vis[y.x][y.y]=1;
for(int i=0;i<4;i++){
int x1=y.x+dx[i],y1=y.y+dy[i];
if(x1<1||x1>n||y1<1||y1>m||pic[x1][y1]=='#')continue;
if(idx[x1][y1]){
if(y.k-idx[x1][y1]-y.cnt<=0){
if(dis[x1][y1]>dis[y.x][y.y]+1){
dis[x1][y1]=dis[y.x][y.y]+1;
q.push({dis[x1][y1],x1,y1,y.k});
}
}else{
continue;
int p=y.k-idx[x1][y1]-y.cnt;
if(dis[x1][y1]>dis[y.x][y.y]+1+p){
dis[x1][y1]=dis[y.x][y.y]+1+p;
q.push({dis[x1][y1],x1,y1,y.k-p});
}
}
}else{
if(dis[x1][y1]>dis[y.x][y.y]+1){
dis[x1][y1]=dis[y.x][y.y]+1;
q.push({dis[x1][y1],x1,y1,y.k});
}
}
}
}
q.push({0,x,y,k});
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
vis[i][j]=0;
}
}
while(!q.empty()){
auto y=q.front();
q.pop();
if(vis[y.x][y.y])continue;
vis[y.x][y.y]=1;
for(int i=0;i<4;i++){
int x1=y.x+dx[i],y1=y.y+dy[i];
if(x1<1||x1>n||y1<1||y1>m||pic[x1][y1]=='#')continue;
if(idx[x1][y1]){
if(y.k-idx[x1][y1]-y.cnt<=0){
if(dis[x1][y1]>dis[y.x][y.y]+1){
dis[x1][y1]=dis[y.x][y.y]+1;
q.push({dis[x1][y1],x1,y1,y.k});
}
// q.push({dis[x1][y1],x1,y1,y.k});
}else{
int p=y.k-idx[x1][y1]-y.cnt;
if(dis[x1][y1]>dis[y.x][y.y]+1+p){
dis[x1][y1]=dis[y.x][y.y]+1+p;
q.push({dis[x1][y1],x1,y1,y.k-p});
}
}
}else{
if(dis[x1][y1]>dis[y.x][y.y]+1){
dis[x1][y1]=dis[y.x][y.y]+1;
q.push({dis[x1][y1],x1,y1,y.k});
}
}
}
}
__int128 ans=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
// cout<<dis[i][j]<<" ";
if(dis[i][j]==1e9)continue;
ans=ans+(dis[i][j]*dis[i][j]);
ans%=mod1;
}
}
cout<<(ull)ans<<endl;
}
signed main() {
std::ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cout<<fixed<<setprecision(11);
int T = 1;
// cin >> T;
for(int i=1;i<=64;i++){
mod1=(ull)mod1*2;
}
while (T--) solve();
return 0;
}
详细
Test #1:
score: 0
Runtime Error
input:
4 5 5 3 5 3 4 3 3 3 2 4 2 ..... ..... ..... .....