답안 #934836

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
934836 2024-02-28T05:08:13 Z 8pete8 로봇 (APIO13_robots) C++17
100 / 100
485 ms 112432 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<cassert>
#include<limits.h>
#include<cmath>
#include<set>
#include<numeric> //gcd(a,b)
#include<algorithm>
#include<bitset> 
#include<stack>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define pppiiii pair<pii,pii>
#define ppii pair<int,pii>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
//#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
//#define int long long
const int mod=1e9+7,mxn=600,lg=30,inf=1e9,minf=-1e18,Mxn=2e6,root=700;
void setIO(string name){
	ios_base::sync_with_stdio(0); cin.tie(0);		
	freopen((name+".in").c_str(),"r",stdin);		
	freopen((name+".out").c_str(),"w",stdout);	
}	 
pii pos[11];
char grid[mxn+10][mxn+10];
int n,h,w;
int dist[mxn*mxn+10],go[mxn+10][mxn+10][5];
vector<int> dp[11][11];
vector<int>adj[mxn*mxn+10];
int get(int x,int y){
	return y+((x-1)*w);
}
int u[4]={1,0,-1,0},r[4]={0,-1,0,1};
bool valid(int x,int y){
	if(x<=0||x>h||y<=0||y>w)return false;
	if(grid[x][y]=='x')return false;
	return true;
}
struct node{
	int pos,stp;
	bool operator<(const node o)const{return stp>o.stp;}
};
int cnt=0;
vector<int>wtf[mxn*mxn+10];
void move(vector<int>&v){
	for(int i=1;i<=v.size();i++)if(v[i]<=h*w)wtf[v[i]].pb(i);
	for(int i=0;i<=h*w;i++){
		while(!wtf[i].empty()){
			int cur=wtf[i].back();
			wtf[i].pop_back();
			if(v[cur]<i)continue;
			for(auto j:adj[cur])if(v[j]>i+1&&i+1<=h*w){
				v[j]=i+1;
				if(v[j]>Mxn)assert(0);
				wtf[v[j]].pb(j);
			}
		}
		wtf[i].shrink_to_fit();
	}
	return;
}
int getnx(int x,int y,int di){
	if(grid[x][y]=='A')di--;
	else if(grid[x][y]=='C')di++;
	di=(di+4)%4;
	if(go[x][y][di])return go[x][y][di];
	int nx=x+u[di],ny=y+r[di];
	if(valid(nx,ny))return go[x][y][di]=getnx(nx,ny,di);
	return go[x][y][di]=get(x,y);
}
//41->21->11
void print(int i,int j){
	for(int x=1;x<=h;x++){
		for(int y=1;y<=w;y++)cout<<((dp[i][j][get(x,y)]==inf)?-1:dp[i][j][get(x,y)])<<" ";
		cout<<'\n';
	}
	cout<<'\n';
}
int32_t main(){
	fastio
	cin>>n>>w>>h;
	int ans=inf;
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)dp[i][j].resize((h+1)*(w+1),inf);
	for(int i=1;i<=h;i++)for(int j=1;j<=w;j++){
		cin>>grid[i][j];
		if(grid[i][j]>='1'&&grid[i][j]<='9')pos[grid[i][j]-'0']={i,j};
	}
	for(int i=1;i<=h;i++)for(int j=1;j<=w;j++)for(int k=0;k<4;k++)adj[get(i,j)].pb(getnx(i,j,k));
	for(int i=1;i<=n;i++){
		dp[i][i][get(pos[i].f,pos[i].s)]=0;
		move(dp[i][i]);
	}
	for(int sz=2;sz<=n;sz++){
		for(int l=1,r=sz;r<=n;l++,r++){
			for(int x=1;x<=h*w;x++){
				for(int mid=l;mid<r;mid++)dp[l][r][x]=min(dp[l][r][x],dp[l][mid][x]+dp[mid+1][r][x]);
			}
			move(dp[l][r]);
		}
	}
	for(int x=1;x<=h*w;x++)ans=min(ans,dp[1][n][x]);
	cout<<((ans==inf)?-1:ans);
}

Compilation message

robots.cpp:34:48: warning: overflow in conversion from 'double' to 'int' changes value from '-1.0e+18' to '-2147483648' [-Woverflow]
   34 | const int mod=1e9+7,mxn=600,lg=30,inf=1e9,minf=-1e18,Mxn=2e6,root=700;
      |                                                ^~~~~
robots.cpp: In function 'void move(std::vector<int>&)':
robots.cpp:62:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |  for(int i=1;i<=v.size();i++)if(v[i]<=h*w)wtf[v[i]].pb(i);
      |              ~^~~~~~~~~~
robots.cpp: In function 'void setIO(std::string)':
robots.cpp:37:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |  freopen((name+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
robots.cpp:38:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |  freopen((name+".out").c_str(),"w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19036 KB Output is correct
2 Correct 6 ms 19036 KB Output is correct
3 Correct 4 ms 19108 KB Output is correct
4 Correct 4 ms 19036 KB Output is correct
5 Correct 4 ms 19036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19036 KB Output is correct
2 Correct 6 ms 19036 KB Output is correct
3 Correct 4 ms 19108 KB Output is correct
4 Correct 4 ms 19036 KB Output is correct
5 Correct 4 ms 19036 KB Output is correct
6 Correct 5 ms 19036 KB Output is correct
7 Correct 4 ms 19036 KB Output is correct
8 Correct 4 ms 19036 KB Output is correct
9 Correct 4 ms 19036 KB Output is correct
10 Correct 4 ms 19036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19036 KB Output is correct
2 Correct 6 ms 19036 KB Output is correct
3 Correct 4 ms 19108 KB Output is correct
4 Correct 4 ms 19036 KB Output is correct
5 Correct 4 ms 19036 KB Output is correct
6 Correct 5 ms 19036 KB Output is correct
7 Correct 4 ms 19036 KB Output is correct
8 Correct 4 ms 19036 KB Output is correct
9 Correct 4 ms 19036 KB Output is correct
10 Correct 4 ms 19036 KB Output is correct
11 Correct 88 ms 52140 KB Output is correct
12 Correct 18 ms 23900 KB Output is correct
13 Correct 41 ms 41108 KB Output is correct
14 Correct 138 ms 52304 KB Output is correct
15 Correct 79 ms 52052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19036 KB Output is correct
2 Correct 6 ms 19036 KB Output is correct
3 Correct 4 ms 19108 KB Output is correct
4 Correct 4 ms 19036 KB Output is correct
5 Correct 4 ms 19036 KB Output is correct
6 Correct 5 ms 19036 KB Output is correct
7 Correct 4 ms 19036 KB Output is correct
8 Correct 4 ms 19036 KB Output is correct
9 Correct 4 ms 19036 KB Output is correct
10 Correct 4 ms 19036 KB Output is correct
11 Correct 88 ms 52140 KB Output is correct
12 Correct 18 ms 23900 KB Output is correct
13 Correct 41 ms 41108 KB Output is correct
14 Correct 138 ms 52304 KB Output is correct
15 Correct 79 ms 52052 KB Output is correct
16 Correct 187 ms 111444 KB Output is correct
17 Correct 485 ms 112432 KB Output is correct
18 Correct 207 ms 111956 KB Output is correct
19 Correct 187 ms 111516 KB Output is correct
20 Correct 324 ms 112180 KB Output is correct