QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#790228 | #9802. Light Up the Grid | walili | WA | 24ms | 8204kb | C++20 | 3.6kb | 2024-11-28 08:50:40 | 2024-11-28 08:50:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int get_board(int status)
{
return status & 0b1111;
}
int get_visited(int status)
{
return status >> 4;
}
int get_status(int board, int visited)
{
return (visited << 4) | board;
}
int operate(int board, int operation)
{
switch (operation)
{
case 0:
return board ^ 0b0001;
case 1:
return board ^ 0b0010;
case 2:
return board ^ 0b0100;
case 3:
return board ^ 0b1000;
case 4:
return board ^ 0b0011;
case 5:
return board ^ 0b1100;
case 6:
return board ^ 0b0101;
case 7:
return board ^ 0b1010;
case 8:
return board ^ 0b1111;
}
}
int get_operation_cost(int operation, const array<int, 4>& a)
{
switch (operation)
{
case 0:
case 1:
case 2:
case 3:
return a[0];
case 4:
case 5:
return a[1];
case 6:
case 7:
return a[2];
case 8:
return a[3];
}
}
void get_min_costs(int board, vector<int>& min_costs, vector<bool>& visited)
{
if (visited[board])
return;
visited[board] = true;
for (int i = 0; i < 16; ++i)
{
int board2 = 0;
if ((board & (1 << i)) != 0)
continue;
board2 = board | (1 << i);
get_min_costs(board2, min_costs, visited);
min_costs[board] = min(min_costs[board], min_costs[board2]);
}
}
int get_board(const string& s1, const string& s2)
{
int result = 0;
result |= s1[0] - '0';
result |= (s1[1] - '0') << 1;
result |= (s2[0] - '0') << 2;
result |= (s2[1] - '0') << 3;
return result;
}
void initialize(vector<int>& min_costs)
{
int board0 = 0, status0 = 0;
array<int, 4> a{};
for (int i = 0; i < 4; ++i)
cin >> a[i];
board0 = 0b1111;
status0 = get_status(board0, 1 << board0);
queue<int> paths;
paths.push(status0);
vector<int> distances(1 << 20, 1 << 30);
distances[status0] = 0;
while (!paths.empty())
{
int path = 0;
path = paths.front();
paths.pop();
for (int operation = 0; operation < 9; ++operation)
{
int board2 = 0, path2 = 0, distance2 = 0;
board2 = operate(get_board(path), operation);
path2 = get_status(board2, get_visited(path) | (1 << board2));
distance2 = distances[path] + get_operation_cost(operation, a);
if (distance2 >= distances[path2])
continue;
paths.push(path2);
distances[path2] = distance2;
}
}
for (int required_boards = 0; required_boards < 1 << 16; ++required_boards)
for (int board = 0; board < 1 << 4; ++board)
min_costs[required_boards] = min(min_costs[required_boards], distances[get_status(board, required_boards)]);
vector<bool> visited(1 << 16);
get_min_costs(0, min_costs, visited);
}
void test_case(const vector<int>& min_costs)
{
int m = 0, required_boards = 0, result = 0;
cin >> m;
for (int i = 0; i < m; ++i)
{
string s1, s2;
cin >> s1 >> s2;
required_boards |= 1 << get_board(s1, s2);
}
result = min_costs[required_boards];
cout << result << '\n';
}
int main()
{
int tests = 0;
cin >> tests;
vector<int> min_costs(1 << 16, 1 << 30);
initialize(min_costs);
while (tests > 0)
{
test_case(min_costs);
--tests;
}
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 24ms
memory: 8204kb
input:
2 1000 100 10 1 4 10 00 01 00 00 10 00 01 1 11 11
output:
1121 0
result:
wrong answer 2nd numbers differ - expected: '2', found: '0'