CF1205C Palindromic Paths
问题分析
首先可以想到,坐标和为奇数的位置可以被唯一确定。同样的,如果假定\((1,2)\)是\(0\),那么坐标和为偶数的位置也可以被唯一确定。这样总共使用了\(n^2-3\)次询问。
那么接下来就需要在\(3\)步之内判断是否要翻转坐标和为偶数的位置。
如果仅仅只是这样简单的判断:
printf( "? 1 1 2 3\n" ); fflush( stdout );
int x; scanf( "%d", &x );
if( x == 1 && A[ 1 ][ 1 ] != A[ 2 ][ 3 ] )
for( int i = 1; i <= n; ++i )
for( int j = 1; j <= n; ++j )
if( ( i + j ) % 2 )
A[ i ][ j ] = ( A[ i ][ j ] == 1 ) ? 0 : 1;
if( x == 0 && A[ 1 ][ 1 ] == A[ 2 ][ 3 ] &&
( A[ 2 ][ 1 ] == A[ 2 ][ 2 ] || A[ 1 ][ 2 ] == A[ 2 ][ 2 ] || A[ 1 ][ 2 ] == A[ 1 ][ 3 ] ) )
for( int i = 1; i <= n; ++i )
for( int j = 1; j <= n; ++ j )
if( ( i + j ) % 2 )
A[ i ][ j ] = ( A[ i ][ j ] == 1 ) ? 0 : 1;
那么就愉快的Wrong Answer on test 5
。
观察这样一组数据:
110
101
110
会发现,不论是否翻转,所得的结果都是\(0\)。
观察发现,翻转会改变这样一条长度为\(4\)的路径上的两个位置。而这样路径上值的异或和是不变的。而长度为\(4\)的\(01\)串异或和为\(0\)是回文的必要条件。如果异或和不为\(0\),就会出现所给反例中的情况。
那么异或和为\(0\)的长度为\(4\)的路径是否一定存在?考虑反证法。
假设不存在这样一条路径。考虑从左上到右下的任意一条路径。这条路径的长度为\(2n-1\)。不妨设这条路径所组成的序列是\(\{a_i\}\)。那么\(a_1 \oplus a_2 \oplus a_3 \oplus a_4=1\),\(a_2\oplus a_3\oplus a_4\oplus a_5=1\)。那么\(a_1=a_5\)。同样的\(a_i=a_{i-4}\)。由于\(n\)是奇数,所以\(a_{2n-1}=a_1\)。 而题目中要求\(a_1=1,a_{2n-1}=0\),所以一定存在这样一条路径。
那么就得到了可行的做法。
参考程序
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 60;
int n, A[ Maxn ][ Maxn ], x;
int Check( int x1, int y1, int x2, int y2 ) {
if( x1 == x2 ) return 1 ^ A[ x1 ][ y1 ] ^ A[ x1 ][ y1 + 1 ] ^ A[ x1 ][ y1 + 2 ] ^ A[ x1 ][ y2 ];
if( x1 + 1 == x2 ) {
int t = A[ x1 ][ y1 ] ^ A[ x2 ][ y2 ];
return ( t == ( A[ x1 + 1 ][ y1 ] ^ A[ x1 + 1 ][ y1 + 1 ] ) ) ||
( t == ( A[ x1 ][ y1 + 1 ] ^ A[ x1 + 1 ][ y1 + 1 ] ) ) ||
( t == ( A[ x1 ][ y1 + 1 ] ^ A[ x1 ][ y1 + 2 ] ) );
}
if( x1 + 2 == x2 ) {
int t = A[ x1 ][ y1 ] ^ A[ x2 ][ y2 ];
return ( t == ( A[ x1 + 1 ][ y1 ] ^ A[ x1 + 2 ][ y1 ] ) ) ||
( t == ( A[ x1 + 1 ][ y1 ] ^ A[ x1 + 1 ][ y1 + 1 ] ) ) ||
( t == ( A[ x1 ][ y1 + 1 ] ^ A[ x1 + 1 ][ y1 + 1 ] ) );
}
if( x1 + 3 == x2 ) return 1 ^ A[ x1 ][ y1 ] ^ A[ x1 + 1 ][ y1 ] ^ A[ x1 + 2 ][ y1 ] ^ A[ x2 ][ y1 ];
return 0;
}
void Swap() {
for( int i = 1; i <= n; ++i )
for( int j = 1; j <= n; ++j )
if( ( i + j ) % 2 )
A[ i ][ j ] = ( A[ i ][ j ] == 1 ) ? 0 : 1;
return;
}
void Trans() {
for( int i = 1; i <= n; ++i )
for( int j = 1; j <= n; ++j )
for( int k = 0; k < 4; ++k )
if( i + k <= n && j + 3 - k <= n && Check( i, j, i + k, j + 3 - k ) ) {
printf( "? %d %d %d %d\n", i, j, i + k, j + 3 - k ); fflush( stdout ); scanf( "%d", &x );
if( x == 1 && A[ i ][ j ] != A[ i + k ][ j + 3 - k ] ) Swap();
if( x == 0 && A[ i ][ j ] == A[ i + k ][ j + 3 - k ] ) Swap();
return;
}
return;
}
int main() {
memset( A, 0, sizeof( A ) );
A[ 1 ][ 1 ] = 1;
scanf( "%d", &n );
for( int i = 1; i <= n; ++i )
for( int j = 1; j <= n; ++j ) {
if( i == 2 && j == 1 ) {
printf( "? 2 1 2 3\n" ); fflush( stdout ); scanf( "%d", &x );
if( x == 0 ) A[ 2 ][ 1 ] = ( A[ 2 ][ 3 ] == 1 ) ? 0 : 1;
if( x == 1 ) A[ 2 ][ 1 ] = ( A[ 2 ][ 3 ] == 1 ) ? 1 : 0;
}
if( i == 1 && j + 2 <= n ) {
printf( "? %d %d %d %d\n", i, j, i, j + 2 ); fflush( stdout ); scanf( "%d", &x );
if( x == 0 ) A[ i ][ j + 2 ] = ( A[ i ][ j ] == 1 ) ? 0 : 1;
if( x == 1 ) A[ i ][ j + 2 ] = ( A[ i ][ j ] == 1 ) ? 1 : 0;
}
if( j == 1 && i + 2 <= n ) {
printf( "? %d %d %d %d\n", i, j, i + 2, j ); fflush( stdout ); scanf( "%d", &x );
if( x == 0 ) A[ i + 2 ][ j ] = ( A[ i ][ j ] == 1 ) ? 0 : 1;
if( x == 1 ) A[ i + 2 ][ j ] = ( A[ i ][ j ] == 1 ) ? 1 : 0;
}
if( i + 1 <= n && j + 1 <= n && i + j + 2 < n + n ) {
printf( "? %d %d %d %d\n", i, j, i + 1, j + 1 ); fflush( stdout ); scanf( "%d", &x );
if( x == 0 ) A[ i + 1 ][ j + 1 ] = ( A[ i ][ j ] == 1 ) ? 0 : 1;
if( x == 1 ) A[ i + 1 ][ j + 1 ] = ( A[ i ][ j ] == 1 ) ? 1 : 0;
}
}
Trans();
printf( "!\n" ); fflush( stdout );
for( int i = 1; i <= n; ++i ) {
for( int j = 1; j <= n; ++j )
printf( "%d", A[ i ][ j ] ), fflush( stdout );
printf( "\n" ); fflush( stdout );
}
return 0;
}
CF1205C Palindromic Paths的更多相关文章
- [USACO15OPEN]回文的路径Palindromic Paths
[USACO15OPEN]回文的路径Palindromic Paths 题目描述 Farmer John's farm is in the shape of an N \times NN×N grid ...
- TOJ 5020: Palindromic Paths
5020: Palindromic Paths Time Limit(Common/Java):10000MS/30000MS Memory Limit:65536KByteTotal Su ...
- 题解 P3126 【[USACO15OPEN]回文的路径Palindromic Paths】
P3126 [USACO15OPEN]回文的路径Palindromic Paths 看到这题题解不多,蒟蒻便想更加通俗易懂地分享一点自己的心得,欢迎大佬批评指正^_^ 像这种棋盘形的两边同时做的dp还 ...
- Educational Codeforces Round 89 (Rated for Div. 2) C Palindromic Paths
题目链接:Palindromic Paths 题意: 给你一个n行m列的矩阵,这个矩阵被0或者1所填充,你需要从点(1,1)走到点(n,m).这个时候会有很多路径,每一条路径对应一个01串,你可以改变 ...
- [USACO15OPEN]回文的路径Palindromic Paths 2.0版
题目描述 农夫FJ的农场是一个N*N的正方形矩阵(2\le N\le 5002≤N≤500),每一块用一个字母作标记.比如说: ABCD BXZX CDXB WCBA 某一天,FJ从农场的左上角走到右 ...
- Palindromic Paths(DP)
描述 Given an N×N grid of fields (1≤N≤500), each labeled with a letter in the alphabet. For example: A ...
- [bzoj4098] [Usaco2015 Open]Palindromic Paths
DP.. f[i][j][k]表示左上结束节点是第i条副对角线上的第j个点,右下结束节点是第n*2-i条副对角线上的第k个点,构成回文的方案数. i那维滚动一下.时间复杂度O(n^3)空间复杂度O(n ...
- [Usaco2015 OPEN] Palindromic Paths
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4098 [算法] 显然 , 回文路径中第i个字母的位置(x , y)必然满足 : x ...
- Codeforces 1205C Palindromic Paths (交互题、DP)
题目链接 http://codeforces.com/contest/1205/problem/C 题解 菜鸡永远做着变巨的梦 然而依然连div1BC题都不会做 要是那天去打cf怕是又要1题滚粗了.. ...
随机推荐
- linux shell脚本中使用expect(脚本打开新终端自动远程连接顺便输一点指令)(巨坑)
放弃吧 我找了六个小时都没找到可以用的方案(指标题括号里的内容) 给个曲线救国的方法: 现把expect脚本写成一个文件 在另一个shell脚本中调用
- Mac 安装 Homebrew
为什么要在 MAC 上安装 Homebrew 它干什么用的呢?我们知道在 CentOS 和 Ubuntu 上都有自己的包管理工具,但是在 MAC 上却没有这样类似的管理工具. # CentOS $ y ...
- JavaScript刷新事件
1, Location reload() 方法 2,
- TOPK 问题
TOPK 问题 描述 如从海量数字中寻找最大的 k 个,这类问题我们称为 TOPK 问题,通常使用堆来解决: 求前 k 大,用最小堆 求前 k 小,用最大堆 例子 现有列表 [1, 2, 0, 3, ...
- windows下使用zookeeper
windows下dos窗口操作:https://blog.csdn.net/a632189007/article/details/78085858
- 剑指offer-数组中重复的数字-数组-python
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- ITCAST-C# 委托
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _12委 ...
- Java APi 之 RMI远程方法调用
一.什么是RPC RPC全称是remote procedure call,即远程过程调用.它是一种协议,用于从远程计算机上请求服务. 例如有两台服务器A和B,A上的应用想要调用B上应用的方法,但是他们 ...
- 06 基本数据结构 - 双端队列(Deque)
一.双端队列(Deque) - 概念:deque(也称为双端队列)是与队列类似的项的有序集合.它有两个端部,首部和尾部,并且项在集合中保持不变. - 特性:deque 特殊之处在于添加和删除项是非限制 ...
- Linux——Session复制中的失败的可能原因之一
组播地址问题 route add -net 224.0.0.0 netmask 240.0.0.0 dev eno16777728(自己的网卡名)