#include #include #include #include #include "validate.h" using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; typedef vector vi; typedef vector vl; typedef long double ld; int n,m; string ABC = "ABC"; int dx[4] = {0,0,-1,1}; int dy[4] = {1,-1,0,0}; bool overlap[201][201]; bool ans[3][201][201]; bool inbounds(int i, int j){ return i >= 0 && j >= 0 && i < n && j < m; } bool connected(int region, int cells, int i, int j){ bool marked[201][201] = {0}; queue Q; Q.push({i, j}); int found = 0; while(!Q.empty()){ int i2 = Q.front().first; int j2 = Q.front().second; Q.pop(); if(!marked[i2][j2]){ marked[i2][j2] = 1; found++; for(int c1 = 0; c1 < 4; c1++){ int i3 = i2 + dx[c1]; int j3 = j2 + dy[c1]; if(inbounds(i3,j3) && !marked[i3][j3] && ans[region][i3][j3]){ Q.push({i3,j3}); } } } } return (found == cells); } void check_case(){ // Read test case: judge_in >> n >> m; for(int c1 = 0; c1 < n; c1++){ string line; judge_in >> line; for(int c2 = 0; c2 < m; c2++){ overlap[c1][c2] = (line[c2] == '2'); } } // Read first line of answer to see if they printed 'impossible'. string first_line; if(!(author_out >> first_line)){ wrong_answer("Could not read first line of contestants output.\n"); } string judge_first_line; judge_ans >> judge_first_line; if(tolower(first_line[0]) == 'i'){ transform(first_line.begin(), first_line.end(), first_line.begin(), ::tolower); if(first_line != "impossible"){ wrong_answer("Contestant probably misspelled 'impossible'.\n"); } if (judge_first_line == "impossible") return; wrong_answer("Contestant printed 'impossible', but an answer existed.\n"); } // Read the remaining lines, check that they have the right format, // and put them in ans[region][row][col]. for(int region = 0; region < 3; region++){ for(int c1 = 0; c1 < n; c1++){ string line; if(region == 0 && c1 == 0){ line = first_line; } else{ if(!(author_out >> line)){ wrong_answer("Language '%c': could not read line %d of contestants output.\n", ABC[region], c1+1); } } if(line.length() != m){ wrong_answer("Language '%c' line %d: invalid length.\n", ABC[region], c1+1); } for(int c2 = 0; c2 < m; c2++){ if(line[c2] != ABC[region] && line[c2] != '.'){ wrong_answer("Language '%c' line %d column %d: invalid character.\n", ABC[region], c1+1, c2+1); } ans[region][c1][c2] = (line[c2] == ABC[region]); } } } // Check that overlaps happen exactly as in the input, and that every cell // belongs to a region. for(int c1 = 0; c1 < n; c1++){ for(int c2 = 0; c2 < m; c2++){ int regions = 0; for(int c3 = 0; c3 < 3; c3++){ regions += int(ans[c3][c1][c2]); } if(regions == 0){ wrong_answer("No language at cell (%d, %d).\n", c1+1, c2+1); } if(regions == 1 && overlap[c1][c2]){ wrong_answer("Only one language at overlap cell (%d, %d).\n", c1+1, c2+1); } if(regions > 1 && !overlap[c1][c2]){ wrong_answer("More than one language at non-overlap cell (%d, %d).\n", c1+1, c2+1); } } } // Check connectivity, and that every region has at least one cell. for(int region = 0; region < 3; region++){ int total = 0; int i_last = -1; int j_last = -1; for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ if(ans[region][i][j]){ total++; i_last = i; j_last = j; } } } if(total == 0){ wrong_answer("Language %c was not spoken anywhere.\n", ABC[region]); } if(!connected(region, total, i_last, j_last)){ wrong_answer("Language %c region was not connected.\n", ABC[region]); } } // Check if judge printed 'impossible': if(judge_first_line == "impossible"){ judge_error("Judge printed 'impossible', but contestants answer was valid.\n"); } } int main(int argc, char **argv) { init_io(argc, argv); check_case(); /* Check for trailing output. */ string trash; if (author_out >> trash) { wrong_answer("Trailing output\n"); } accept(); return 0; }