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

 

1080번: 행렬

첫째 줄에 행렬의 크기 N M이 주어진다. N과 M은 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에는 행렬 A가 주어지고, 그 다음줄부터 N개의 줄에는 행렬 B가 주어진다.

www.acmicpc.net

흠 그리드  문제도 이제 조금 익숙해지네요. 그래도 어렵네요 ㅎ

#include <iostream>
#include <vector>
#include <queue>
#include<string>
#include<algorithm>
#include<cmath>
#include<unordered_map>
#include<map>
#include<stack>
using namespace std;

struct Point {
	int s, e, t, cnt, power;
	char c;
};

void Prints(vector<vector<int>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}

void Prints(vector<vector<bool>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}
void Prints(vector<vector<char>>maps, int n, int m) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << maps[i][j];
		}
		cout << endl;
	}
	cout << endl;
}
void Prints(vector<vector<vector<char>>>maps, int n, int m, int k) {
	for (int t = 0; t < k; t++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cout << maps[t][i][j];
			}
			cout << endl;
		}
		cout << endl;
	}
	cout << endl;
}

void Prints(vector<vector<vector<bool>>>maps, int n, int m, int k) {
	for (int t = 0; t < k; t++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cout << maps[t][i][j];
			}
			cout << endl;
		}
		cout << endl;
	}
	cout << endl;
}
void Prints(vector<int>line, int n) {
	for (int i = 0; i < n; i++) {
		cout << line[i] << ",";
	}
	cout << endl;
}
void Prints(string str, int n) {
	for (int i = 0; i < n; i++) {
		cout << str[i] << ",";
	}
	cout << endl;
}

bool checkmaps(vector<vector<int>>maps, vector<vector<int>>anw, int n, int m) {
	bool bCheck = false;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (maps[i][j] != anw[i][j]) {
				bCheck = true;
				i = n + 1;
				j = m + 1;
				continue;
			}
		}

	}
	return bCheck;

}
bool findstart(vector<vector<int>>maps, vector<vector<int>>anw, int n, int m, int) {
	bool bCheck = false;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (maps[i][j] != anw[i][j]) {
				bCheck = true;
				i = n + 1;
				j = m + 1;
				continue;
			}
		}

	}
	return bCheck;

}

struct Data {
	int index;
	int score;
};


bool cmp(const Data& a, const Data& b) {
	if (a.score == b.score) {
		return a.index < b.index;
	}
	return a.score > b.score;
}

int main() {

	int n, m;
	cin >> n >> m;
	vector<vector<int>>maps;
	vector<vector<int>>anw;
	//3x3
	//행렬
	for (int i = 0; i < n; i++) {
		vector<int>tmpmaps;
		//string str;
		//cin >> str;
		for (int j = 0; j < m; j++) {
			char c;
			cin >> c;
			tmpmaps.push_back(c - '0');
		}
		maps.push_back(tmpmaps);
	}
	for (int i = 0; i < n; i++) {
		vector<int>tmpmaps;
		//string str;
		//cin >> str;
		for (int j = 0; j < m; j++) {
			char c;
			cin >> c;
			tmpmaps.push_back(c - '0');
		}
		anw.push_back(tmpmaps);
	}
	//cout << endl;
	//Prints(maps, n, m);
	//cout << endl;
	//Prints(anw, n, m);
	//cout << endl;


	int mxi_s = 0;
	int mxi_e = 3;

	int mxj_s = 0;
	int mxj_e = 3;
	int cnt = 0;
	int gcnt = -1;
	bool bEND = checkmaps(maps, anw, n, m);
	if (bEND == false) {
		cout << 0;
		return 0;
	}
	if (n < 3 || m < 3) {
		cout << -1;
		return 0;
	}
	//bEND = checkmaps(maps, anw, n, m);
	bool bFinish = false;
	
	int i = 0;
		while (true) {
			if (i >n||bFinish==true) {
				break;
				
			}
			
		            //i=1
				if (maps[i][mxj_s] == anw[i][mxj_s]) {
					//첫번째가 같은경우
					int correctCnt = 0;
					for (int j = mxj_s; j < mxj_e; j++) {
						if (maps[i][j] == anw[i][j]) {
							correctCnt++;
						}
						else if (maps[i][j] != anw[i][j]) {
							j = mxj_e+1;
							continue;
						}
					}
					
					mxj_s = mxj_s + correctCnt;//0 3
					mxj_e = mxj_e + correctCnt;//3 6
					
					if (mxj_e > m) {
						mxj_e = 3;//3
						mxj_s = 0;//0
						mxi_s = mxi_s + 1;//0
						mxi_e = mxi_e + 1;//1
						if (mxi_e > n) {
							mxi_s = n-3;//0
							mxi_e = n;//1
							bFinish = true;
						}
						i++;
					}
					
					
				}
			    else if (maps[i][mxj_s] != anw[i][mxj_s]) {
					int incorrectCnt = 0;
					for (int j = mxj_s; j < mxj_e; j++) {
						if (maps[i][j] != anw[i][j]) {
							incorrectCnt++;
						}
					}
					//변경
					for (int t = mxi_s; t < mxi_e; t++) {// 1 4 
						for (int j = mxj_s; j < mxj_e; j++) {//0 3
							if (maps[t][j] == 0) {
								maps[t][j] = 1;
							}
							else if (maps[t][j] == 1) {
								maps[t][j] = 0;
							}
						}
					}
					cnt++;
					//cout << endl;
					bEND = checkmaps(maps, anw, n, m);
					if (bEND == false) {
						i = n+1;
						continue;
					}
					//Prints(maps, n, m);
					
					//변경후 이동
					if (incorrectCnt == 3) {
						mxj_s = mxj_s + 3;
						mxj_e = mxj_e + 3;
					}
					else {//그렇치 않는경우
						mxj_s = mxj_s + 1;
						mxj_e = mxj_e + 1;
						
					}

					if (mxj_e > m) {
						if (incorrectCnt != 3) {
							bFinish = true;
						}
						mxj_e = 3;
						mxj_s = 0;
						mxi_s = mxi_s + 1;
						mxi_e = mxi_e + 1;
						if (mxi_e > n) {
							mxi_s = n - 3;//0
							mxi_e = n;//1
							bFinish = true;
						}
						i++;
						
					}
					

				}

				
		}
		
	

	if (bEND == true) {
		cout << -1;
	}
	else {
		cout << cnt;
	}
	return 0;
}

'알고리즘 공부' 카테고리의 다른 글

백준 랭킹전 대기열  (0) 2023.08.04
백준 특정거리의 도시 찾기  (0) 2023.08.04
백준 NBA 농구  (0) 2023.08.03
백준 효율적인 해킹  (0) 2023.08.02
백준 그림  (0) 2023.08.02

+ Recent posts