您的位置:首页 > 大数据 > 人工智能

poj-1222-EXTENDED LIGHTS OUT && poj-1681-Painter's Problem

2016-10-05 10:08 232 查看
gauss消元

这两道题都和poj-1830类似

只不过把二维矩阵看成一维的,每一个开关会影响它周围的灯的状态。

poj-1222-EXTENDED LIGHTS OUT

一发ac

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
const int inf = 0x3fffffff;
const double eps = 1e-6;
const int N = 33;
int res, r;
int a

;
void debug(){
return ;
cout << "---------beautiful delimiter start------------" << endl;
for (int i = 0; i < 30; i++){
for (int j = 0; j <= 30; j++){
cout << a[i][j] << " ";
}
cout << endl;
}
cout << "---------beautiful delimiter end------------" << endl;
}
int gauss(int a[]
) {
int res = 0, r = 0;
debug();
for (int i = 0; i < 30; i++){
for (int j = r; j < 30; j++){
if (a[j][i]){
for (int k = i; k <= 30; k++){
swap(a[j][k], a[r][k]);
}
break;
}
}
if (!a[r][i]){
res++;
continue;
}
for (int j = 0; j < 30; j++){
if (j != r && a[j][i]){
for (int k = i; k <= 30; k++){
a[j][k] ^= a[r][k];
}
}
}
r++;
//      debug();
}
debug();
}

int main(){
#ifndef ONLINE_JUDGE
freopen("1.txt", "r", stdin);
#endif
int i, j, k, t;
cin >> t;
for (int cas = 1; cas <= t; cas++){
memset(a, 0, sizeof(a));
for (i = 0; i < 30; i++){
cin >> a[i][30];
}
for (i = 0; i < 5; i++){
for (j = 0; j < 6; j++){
if (i != 0){
// up
a[(i-1)*6+j][i*6+j] = 1;
}
if (j != 0){
// left
a[i*6+j-1][i*6+j] = 1;
}
if (i != 4){
// down
a[(i+1)*6+j][i*6+j] = 1;
}
if (j != 5){
// right
a[i*6+j+1][i*6+j] = 1;
}
}
}
for (i = 0; i < 30; i++){
a[i][i] = 1;
}
gauss(a);
cout << "PUZZLE #" << cas << endl;
for (i = 0; i < 5; i++){
for (j = 0; j < 6; j++){
cout << a[i*6+j][30] << " ";
}
cout << endl;
}
}
return 0;
}


poj-1681-Painter’s Problem

wa了一次,忘记考虑inf的情况了

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
const int inf = 0x3fffffff;
const double eps = 1e-6;
const int N = 330;
int res, r;
int a

;
void debug(){
return ;
cout << "---------beautiful delimiter start------------" << endl;
for (int i = 0; i < 25; i++){
for (int j = 0; j <= 25; j++){
cout << a[i][j] << " ";
}
cout << endl;
}
cout << "---------beautiful delimiter end------------" << endl;
}
int gauss(int a[]
, int n) {
int res = 0, r = 0;
n *= n;
debug();
for (int i = 0; i < n; i++){
for (int j = r; j < n; j++){
if (a[j][i]){
for (int k = i; k <= n; k++){
swap(a[j][k], a[r][k]);
}
break;
}
}
if (!a[r][i]){
res++;
continue;
}
for (int j = 0; j < n; j++){
if (j != r && a[j][i]){
for (int k = i; k <= n; k++){
a[j][k] ^= a[r][k];
}
}
}
r++;
//      debug();
}
debug();
for (int i = r; i < n; i++){
if (a[i]
!= 0){
return 0;
}
}
}

int main(){
#ifndef ONLINE_JUDGE
freopen("1.txt", "r", stdin);
#endif
int i, j, k, t, n;
cin >> t;
char c;
int ans;
for (int cas = 1; cas <= t; cas++){
memset(a, 0, sizeof(a));
cin >> n;
for (i = 0; i < n*n; i++){
cin >> c;
if (c == 'y'){
a[i][n*n] = 0;
}else{
a[i][n*n] = 1;
}
}
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
if (i != 0){
// up
a[(i-1)*n+j][i*n+j] = 1;
}
if (j != 0){
// left
a[i*n+j-1][i*n+j] = 1;
}
if (i != n-1){
// down
a[(i+1)*n+j][i*n+j] = 1;
}
if (j != n-1){
// right
a[i*n+j+1][i*n+j] = 1;
}
}
}
for (i = 0; i < n*n; i++){
a[i][i] = 1;
}
if (!gauss(a, n)){
cout << "inf" << endl;
continue;
}
ans = 0;
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
ans += a[i*n+j][n*n];
}
}
cout << ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: