POJ 1753 高斯消元

2021-07-08 17:26:28 字數 1309 閱讀 2055

題意是把所有的字母都變成w或者b的最小步數.

跑兩次01方程,取最小如果兩次都無解就輸出無解.板子題~

#include #include #include #include #include using namespace std;

#define maxn 333

#define free free

#define n 4

int a[maxn][maxn];

char mp[maxn][maxn];

int free[maxn*maxn]; //標記自由元

int x[maxn*maxn], cnt; //解集 自由元個數

int gauss ()

if (max_r != k)

if (a[k][col] == 0)

for (int i = k+1; i < n*n; i++) }}

}for (int i = k; i < n*n; i++)

if (k < n*n)

return n*n - k; //返回自由元個數

for (int i = n*n-1; i >= 0; i--)

return 0;

}int solve ()

else if (tot == 0)

else

num += x[id];

}ans= min (ans, num);

}return ans;

}}int main ()

memset (a, 0, sizeof a);

for (int i = 0; i < n; i++)

}for (int i = 0; i < n; i++)

}int ans1 = solve ();

memset (a, 0, sizeof a);

for (int i = 0; i < n; i++)

}for (int i = 0; i < n; i++)

}int ans2 = solve ();

if (ans1 == -1 && ans2 == -1)

cout << "impossible" << endl;

else if (ans1 == -1)

cout << ans2 << endl;

else if (ans2 == -1)

cout << ans1 << endl;

else cout << min (ans1, ans2) << endl;

}return 0;

}

高斯消元(poj1753

參考了kuangbin的部落格,注意自由變元那邊 include include include include includeusing namespace std const int maxn 30 const int inf 0x3fffffff int a maxn maxn 增廣矩陣 in...

POJ 1753 開關問題 高斯消元法

題目鏈結 題目大意 一堆格仔,或白或白。每次可以把乙個改變乙個格仔顏色,其上下左右四個格仔顏色也改變。問最後使格仔全部白或全部黑,求最小改變的格仔樹。解題思路 與poj 1681 類似。不過這次是或黑或白,要初始化兩次相反的解向量,進行兩次高斯消元,取其中小的值。特殊的是,本題中有自由變元的存在,也...

poj 1753 Flip Game 高斯消元

因為目標狀態是全白或全黑,所以進行兩次高斯消元,每次若有自由變元的話要列舉自由變元求得最優解。哇哦,怎麼就寫了200 行。include include include include include include include include include include include i...