https://www.acmicpc.net/problem/17081

이건데


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>

#include <stack>

#include <cstring>

using namespace std;

int n, m; // 보드 크기

bool win;

char** board; // 게임 보드


typedef struct data1{ // 캐릭터 구조체

    int max_hp=0;

    int now_hp=0;

    int att=0;

    int def=0;

    int level=0;

    int exp=0;

    int weapon=0;

    int armor=0;

    char accessories[4][3];

    int start_x;

    int start_y;

    char death_reason[10];

}character;

character user; // 유저의 구조체

typedef struct data2{ // 정보 구조체

    char name[10]=" ";

    int att=0;

    int now_hp = 0;

    int max_hp=0;

    int def=0;

    int exp=0;

    

    char type=' ';

    int weapon=0;

    int armor=0;

    char acc[3]=""; // 악세서리라면

}information;

information** information_board; // 정보가 기록된 보드


void way(int* x, int* y, char location);

void item_box(int x,int y);

void thorn_trap(int x,int y);

void monster(int x,int y);

void boss_monster(int x,int y);

bool check_accessories(const char* acc);


int main() {

    bool trap=false;

    int cnt=0; // 보드 안에 있는 아이템,몬스터 개수들

    int idx = 0; // waypoint의 인덱스

    int x = 0, y = 0; // 유저의 좌표

    char waypoint[5001]; // 방향 받는 거

    cin >> n >> m;

    

    

    information_board = (information**)malloc(sizeof(information*) * n+1);

    for (int i = 0; i < n+1; i++) {

        information_board[i] = (information*)malloc(sizeof(information) * m+1);

    }

    board = (char**)malloc(sizeof(char*) * n);

    for (int i = 0; i < n; i++) {

        board[i] = (char*)malloc(sizeof(char) * m);

    }

    for (int i = 0; i < n; i++) { // 게임 보드 받는 코드

        for (int j = 0; j < m; j++) {

            cin >> board[i][j];

            if (board[i][j] == '@') {

                x = j;

                y = i;

            }

            if (board[i][j] == '&' || board[i][j] == 'B' || board[i][j] =='M') {

                cnt++;

            }

        }

    }

    cin >> waypoint; // 가는길 입력받기

    for (int i = 0; i < cnt; i++) { // 게임 안의 아이템 값들 받는 것

        int a, b; // 위치값

        char c[10]; // 아이템이나 몬스터 받는 구별 값

        cin >> a >> b >> c;

        a = a - 1;

        b = b - 1;

        if (strcmp(c, "W")==0) { // 무기

            information_board[a][b].type = 'W';

            cin >> information_board[a][b].weapon;

        }

        else if (strcmp(c, "A")==0) { // 갑옷

            information_board[a][b].type = 'A';

            cin >> information_board[a][b].armor;

        }

        else if (strcmp(c, "O") == 0) { // 장신구

            information_board[a][b].type = 'O';

            cin >> information_board[a][b].acc;

        

        else { // 나머지는 무조건 몬스터

            strcpy(information_board[a][b].name, c);

            cin >> information_board[a][b].att >> information_board[a][b].def >> information_board[a][b].max_hp >> information_board[a][b].exp;

            information_board[a][b].now_hp = information_board[a][b].max_hp;

        }

    }

    // 게임 시작 전 유저의 정보값 주기.

    user.max_hp = 20;

    user.att = 2;

    user.def = 2;

    user.level = 1;

    user.armor = 0;

    user.weapon = 0;

    user.exp = 0;

    user.now_hp = 20;

    user.start_x = x;

    user.start_y = y;

    win = false;

    strcpy(user.death_reason, " ");

    for (int i = 0; i < 4; i++) {

        strcpy(user.accessories[i]," ");

    }

    // 게임 시작

    while (1) {

        if (idx > strlen(waypoint)-1) {

            break;

        }

        /*cout << y << ' ' << x << '\n';

        for (int i = 0; i < n; i++) {

            for (int j = 0; j < m; j++) {

                cout << board[i][j];

            }

            cout << '\n';

        }

        cout << "Passed Turns : " << idx << '\n';

        cout << "LV : " << user.level << '\n';

        cout << "HP : " << user.now_hp << "/" << user.max_hp << '\n';

        cout << "ATT : " << user.att << "+" << user.weapon << '\n';

        cout << "DEF : " << user.def << "+" << user.armor << '\n';

        cout << "EXP : " << user.exp << "/" << user.level * 5 << '\n';

        for (int i = 0; i < 4; i++) {

            cout << "Acc " << i+1 << "번 " << user.accessories[i] << '\n';

        }*/

        if (trap == true) {

            board[y][x] = '^';

            trap = false;

        }

        else {

            board[y][x] = '.';

        }

        way(&x, &y, waypoint[idx]);

        idx++;

        if (board[y][x] == '&') {

            monster(x,y);

        }

        if (board[y][x] == 'B') {

            item_box(x, y);

        }

        if (board[y][x] == '^') {

            thorn_trap(x, y);

            trap = true;

        }

        if (board[y][x] == 'M') {

            boss_monster(x, y);

        }

        if (user.now_hp <= 0 && check_accessories("RE") == true) {

            x = user.start_x;

            y = user.start_y;

            user.now_hp = user.max_hp;

            for (int i = 0; i < 4; i++) {

                if (strcmp(user.accessories[i], "RE")==0) {

                    strcpy(user.accessories[i]," ");

                }

            }

            trap = false;

        }

        if (user.now_hp <= 0 && check_accessories("RE") == false) {

            break;

        }

        board[y][x] = '@';

        

        if (win == true) {

            break;

        }

    }

    for (int i = 0; i < n; i++) {

        for (int j = 0; j < m; j++) {

            cout << board[i][j];

        }

        cout << '\n';

    }

    if (user.now_hp < 0) {

        user.now_hp = 0;

    }

    cout << "Passed Turns : " << idx << '\n';

    cout << "LV : " << user.level << '\n';

    cout << "HP : " << user.now_hp << "/" << user.max_hp << '\n';

    cout << "ATT : " << user.att << "+" << user.weapon << '\n';

    cout << "DEF : " << user.def << "+" << user.armor << '\n';

    cout << "EXP : " << user.exp << "/" << user.level * 5 << '\n';

    

    if(win == true) {

        cout << "YOU WIN!";

    }

    else if (strcmp(user.death_reason, " ") != 0) {

        cout << "YOU HAVE BEEN KILLED BY " << user.death_reason << "..";

    }

    else{

        cout << "Press any key to continue.";

    }

}


void monster(int x,int y) {


    int first = 1;

    // 전투 페이즈

    while (1) {

        /*cout << "유저의 상태" << '\n';

        cout << "HP : " << user.now_hp << "/" << user.max_hp << '\n';

        cout << "ATT : " << user.att << "+" << user.weapon << '\n';

        cout << "DEF : " << user.def << "+" << user.armor << '\n';

        

        cout << information_board[y][x].name << "의 상태" << '\n';

        cout << "HP : " << information_board[y][x].now_hp << "/" << information_board[y][x].max_hp << '\n';

        cout << "ATT : " << information_board[y][x].att << '\n';

        cout << "DEF : " << information_board[y][x].def << '\n';*/


        if (first == 1) {

            if (check_accessories("CO") == true && check_accessories("DX") == true) {

                information_board[y][x].now_hp -= max(1, (user.att + user.weapon) * 3 - information_board[y][x].def);


            }

            else if (check_accessories("CO") == true && check_accessories("DX") == false) {

                information_board[y][x].now_hp -= max(1, (user.att + user.weapon) * 2 - information_board[y][x].def);

            }

            else {

                information_board[y][x].now_hp -= max(1, user.att + user.weapon - information_board[y][x].def);

            }

            

        }

        else {

            information_board[y][x].now_hp -= max(1, user.att + user.weapon - information_board[y][x].def);

        }

        if (information_board[y][x].now_hp <= 0) {

            break;

        }

        user.now_hp -= max(1, information_board[y][x].att - user.def - user.armor);

        if (user.now_hp <= 0) {

            if (check_accessories("RE") == true) {

                information_board[y][x].now_hp = information_board[y][x].max_hp;

            }

            else {

                strcpy(user.death_reason,information_board[y][x].name);

            }

            return;

        }

        first = 0;

    }


    if (check_accessories("HR") == true) {

        if (user.now_hp + 3 > user.max_hp) {

            user.now_hp = user.max_hp;

        }

        else {

            user.now_hp = user.now_hp + 3;

        }

    }

    if (check_accessories("EX") == true) {

        user.exp += int(information_board[y][x].exp*1.2);

    }

    else {

        user.exp += information_board[y][x].exp;

    }

    if (user.exp >= user.level * 5) {

        user.exp = 0;

        user.level += 1;

        user.max_hp += 5;

        user.now_hp = user.max_hp;

        user.att += 2;

        user.def += 2;

    }



    return;

}


bool check_accessories(const char* acc) {

    bool ischeck=0;

    for (int i = 0; i < 4; i++) {

        if (strcmp(user.accessories[i], acc) == 0) {

            ischeck = 1;

        }

    }

    return ischeck;

}


void item_box(int x,int y) {

    if (information_board[y][x].type == 'W') {

        user.weapon = information_board[y][x].weapon;

    }

    if (information_board[y][x].type == 'A') {

        user.armor= information_board[y][x].armor;

    }

    if (information_board[y][x].type == 'O') {

        for (int i = 0; i < 4; i++) {

            if (strcmp(user.accessories[i], " ")==0) {

                if (check_accessories(information_board[y][x].acc) == false) {

                    strcpy(user.accessories[i], information_board[y][x].acc);

                }

            }

        }

    }

    return;

}


void boss_monster(int x,int y) {

    int first = 1;

    if (check_accessories("HU") == true) {

        user.now_hp = user.max_hp;

    }

    //전투 페이즈

    while (1) {

        if (first == 1) {

            if (check_accessories("CO") == true && check_accessories("DX") == true) {

                information_board[y][x].now_hp -= max(1, (user.att + user.weapon) * 3 - information_board[y][x].def);

                

            }

            else if (check_accessories("CO") == true && check_accessories("DX") == false) {

                information_board[y][x].now_hp -= max(1, (user.att + user.weapon) * 2 - information_board[y][x].def);

            }

            else {

                information_board[y][x].now_hp -= max(1, user.att + user.weapon - information_board[y][x].def);

            }

        }

        else {

            information_board[y][x].now_hp -= max(1, user.att + user.weapon - information_board[y][x].def);

        }

        if (information_board[y][x].now_hp <= 0) {

            win = true;

            break;

        }

        if (first == 1 && check_accessories("HU") == true) {

            user.now_hp -= 0;

        }

        else {

            user.now_hp -= max(1, information_board[y][x].att - user.def - user.armor);

        }

        if (user.now_hp <= 0) {

            if (check_accessories("RE") == true) {

                information_board[y][x].now_hp = information_board[y][x].max_hp;

            }

            else {

                strcpy(user.death_reason, information_board[y][x].name);

            }

            return;

        }

        first = 0;

    }

    if (check_accessories("HR") == true) {

        if (user.now_hp + 3 > user.max_hp) {

            user.now_hp = user.max_hp;

        }

        else {

            user.now_hp = user.now_hp + 3;

        }

    }

    if (check_accessories("EX") == true) {

        user.exp += int(information_board[y][x].exp * 1.2);

    }

    else {

        user.exp += information_board[y][x].exp;

    }

    if (user.exp >= user.level * 5) {

        user.exp = 0;

        user.level += 1;

        user.max_hp += 5;

        user.now_hp = user.max_hp;

        user.att += 2;

        user.def += 2;

    }

    return;

}


void thorn_trap(int x, int y) {

    

    if (check_accessories("DX") == true) {

        user.now_hp -= 1;

    }

    else {

        user.now_hp -= 5;

    }

    if (user.now_hp <= 0) {

        if (check_accessories("RE") != true) {

            strcpy(user.death_reason, "SPIKE TRAP");

        }

        return;

    }

}


void way(int* x, int* y, char location) {

    if (*x < m - 1) { 

        if (board[*y][*x + 1] != '#' && location == 'R') {

            ++* x;

        }

    }

    if (*x - 1 >= 0) {

        if (board[*y][*x - 1] != '#' && location == 'L') {

            --* x;

        }

    }

    if (*y < n - 1) {

        if (board[*y + 1][*x] != '#' && location == 'D') {

            ++* y;

        }

    }

    if (*y - 1 >= 0) {

        if (board[*y - 1][*x] != '#' && location == 'U') {

            --* y;

        }

    }

    return;

}

이렇게 짰는데 37퍼까지만 맞고 segmentation fault 뜨는데 segmentation fault가 null 메모리 가리켰을때 보통 뜬다는걸로

아는데 어디인지를 모르겠음... 봐줄사람

그냥 심심해서 풀어보다가 밤 샜다