[UVA11205] The broken pedometer, train of thought + Code, may be The simplest, most understandable, and most efficient code.

Source: Internet
Author: User
# Include <cstdlib> # include <cstdio> # include <cstring> # include <algorithm> # include <queue> using namespace std;/** Problem: UVA11205-The broken pedometer Begin Time: 28th/Mar/2012 a.m. finish Time: 29th/Mar/2012 a.m. last Time: About 6 hours. state: 992440711205The broken pedometerAcceptedC ++ 2.6842012-03-28 18:11:32 knodge DGE: BFS, No pruning, and subset generation. Is there a small State compression? Exp ..: WA * 4: The four Wrong Answer gave a mistake to write BFS as DFS. It shouldn't be WA * 4: I am not capable of talking about the last four WA. Boundary, boundary! Tmp. depth + 1 <= col is written as tmp. depth + 1 <col, And a combination is missing! An idiot! When writing a program. Operation. Thought: replace each line with an integer like 10110101 (because it can only be 2 ^ 15 at most, it does not need bitwise Operations, otherwise it is a strange problem) to open an array isSel [], if the ith column is selected, isSel [I] = true; then the selected number of columns is determined based on isSel, And the subsets should be extracted from those columns. In fact, bit operations can be faster. Summerize: 1. think carefully about the search process, the status transfer method, whether there are status nodes not expanded 2. think about the boundary. The most important question is whether there is a problem. I can see it clearly before writing it. Generally, there are shortest, minimum, and minimum BFS words. if a tree is searched, no backtracking is required. If a graph is searched, backtracking is required. */Const int MAXN = 200; const int MAXP = 50; int puzzle [MAXN] [MAXP]; int subset [MAXN]; bool isSel [MAXN]; int minN; bool isFound; struct node {int depth; bool isSel [MAXN] ;}; node tmp; queue <node> que; int comp (const void * a, const void * B) {return (* (int *) a-* (int *) B);} bool check (int row, int col) {int cnt = 0; memset (subset, 0, sizeof (subset); for (int I = 0; I <col; I ++) {if (isSel [I] = true) cnt ++;} (Int j = 0; j <row; j ++) {for (int I = 0; I <col; I ++) {if (isSel [I] = true) {subset [j] = subset [j] * 10 + puzzle [j] [I] ;}} qsort (subset, row, sizeof (int), comp); int I = 0; int j = I + 1; while (j <row) {if (subset [I] = subset [j]) {return false;} I ++, j ++;} if (minN> cnt) {minN = cnt ;} return true;} int Solve (int row, int col, int now) {node tmp, tmp1, k; while (! Que. empty () {tmp = que. front (); que. pop (); memcpy (isSel, tmp. isSel, sizeof (isSel); tmp1 = tmp; check (row, col); if (tmp. depth + 1 <= col) {tmp1.depth = tmp. depth + 1; tmp1.isSel [tmp1.depth-1] = false; que. push (tmp1); tmp1.isSel [tmp1.depth-1] = true; que. push (tmp1) ;}}return minN ;}int main () {# ifndef ONLINE_JUDGE freopen ("B: \ acm \ ultraviolet \ UVA11205 \ input.txt ", "r", stdin); freopen ("B: \ acm \ ultraviolet \ uva11 205 \ output.txt "," w ", stdout); # endif int T, P, N, k; // while (scanf (" % d ", & T )! = EOF) scanf ("% d", & T); {for (int t = 1; t <= T; t ++) {scanf ("% d", & P, & N); memset (puzzle, 0, sizeof (puzzle); memset (subset, 0, sizeof (subset); memset (isSel, 0, sizeof (isSel); for (int I = 0; I <N; I ++) {for (int j = 0; j <P; j ++) {scanf ("% d", & puzzle [I] [j]) ;}} minN = 200; tmp. depth = 1; tmp. isSel [0] = true; que. push (tmp); tmp. depth = 1; tmp. isSel [0] = false; que. push (tmp); k = Solve (N, P, 0 ); Printf ("% d \ n", k); while (! Que. empty () {que. pop () ;}} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.