#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector <vector<vector<int>>> gun;
vector <vector<int>> people;
vector <vector<int>> info;
int N,M,K;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
void changeGun(int player,int x, int y){
if(gun[x][y].size()==0)return;
if(gun[x][y][0]>info[player][4]){
int tmp = info[player][4];
info[player][4]=gun[x][y][0];
gun[x][y][0]=tmp;
sort(gun[x][y].begin(), gun[x][y].end(),greater<int>());
}
}
void fight(int p1, int p2, int x, int y){
int p1power=info[p1][3]+info[p1][4];
int p2power=info[p2][3]+info[p2][4];
int winner=p1, looser=p2;
if(p1power==p2power){
if(info[p1][3]<info[p2][3]) {
winner=p2;
looser=p1;
}
} else if(p1power<p2power){
winner=p2;
looser=p1;
}
int score=(p1power-p2power);
if (score<0)score*=-1;
info[winner][5]+=score;
if(info[looser][4]!=0){
gun[x][y].push_back(info[looser][4]);
info[looser][4]=0;
sort(gun[x][y].begin(), gun[x][y].end(),greater<int>());
}
changeGun(winner,x,y);
int nx,ny,nd;
int d=info[looser][2];
for(int i=0;i<4;i++){
nd=(d+i)%4;
nx=x+dx[nd];
ny=y+dy[nd];
if(nx >= 1 && nx <= N && ny >= 1 && ny <= N && people[nx][ny] == 0){
info[looser][0]=nx;
info[looser][1]=ny;
info[looser][2]=nd;
people[nx][ny]=looser;
changeGun(looser,nx,ny);
break;
}
}
info[winner][0]=x;
info[winner][1]=y;
people[x][y]=winner;
}
void move(int player){
int d=info[player][2];
int nx=info[player][0]+dx[d];
int ny=info[player][1]+dy[d];
if(nx<1||nx>N||ny<1||ny>N){
d=(d+2)%4;
info[player][2]=d;
nx=info[player][0]+dx[d];
ny=info[player][1]+dy[d];
}
if(people[nx][ny]==0){
people[info[player][0]][info[player][1]]=0;
people[nx][ny]=player;
info[player][0]=nx;
info[player][1]=ny;
changeGun(player,nx,ny);
}
else {
people[info[player][0]][info[player][1]]=0;
fight(player,people[nx][ny],nx,ny);
}
}
int main() {
int a;
cin>>N>>M>>K;
gun.resize(N+1,vector<vector<int>>(N+1));
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
cin>>a;
if(a!=0)gun[i][j].push_back(a);
}
}
info.resize(M+1,vector<int>(6,0));
people.resize(N+1,vector<int>(N+1));
for(int i=1;i<=M;i++){
cin>>info[i][0]>>info[i][1]>>info[i][2]>>info[i][3];
people[info[i][0]][info[i][1]]=i;
}
for(int i=1;i<=K;i++){
for(int j=1;j<=M;j++){
move(j);
}
}
for(int i=1;i<=M;i++){
cout<<info[i][5]<<" ";
}
return 0;
}
- 총바꾸는 로직을 changeGun함수로 안빼고 중복된 코드를 계속 작성했었다.
- gun을 sort해서 관리할 생각을 못해서 계속 push_back 하고 find해서 erase했었다. 비효율적이다.
- ⭐️ 싸움 후 진사람 방향 바꿀 때 for문 안에서 새로운 방향을 nd에 저장하는게 아니라 d에 저장해서 계속 업데이트 했다. 그럼 당연히 방향이 제대로 설정이 안된다.... 이거때문에 틀린 부분 몇시간을 찾았다.