"NOIP 2004" Insect Food Calculation

Source: Internet
Author: User

Because the machine room is broken network state, the school's small v evaluation of the total display unaccept, so in the afternoon after school and Xiaoyimi evening home study, to do this problem.

Search + Pruning Optimization:

At first I was down to the high-level fill number, so violent on the Vijos have 90 points, if Noip can get so many points I also willingly write this violence

#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int s1[27], s2[27], an[27 ], ans[27], N;bool vis[27], PD = 0;inline void dfs1 (int, int), inline void dfs3 (int tmp, int delta) {int c = an[tmp];if (an  S[C] = =-1) {int num = (ans[s1[tmp]] + ans[s2[tmp]] + delta), MO = num% n;if (!vis[mo]) {Vis[mo] = 1;ans[c] = MO;DFS1 (tmp + 1, num/n); if (PD) RETURN;VIS[MO] = 0;ans[c] =-1;}} else {int num = (ans[s1[tmp]] + ans[s2[tmp]] + delta), if (ans[c] = = num% n) DFS1 (tmp + 1, num/n), if (PD) return;;}  inline void dfs2 (int tmp, int delta) {int c = s2[tmp];if (ans[c] = =-1) {for (int i = 0; i < n; ++i) if (!vis[i]) {Vis[i] = 1;ans[c] = I;DFS3 (tmp, Delta), if (PD) return;vis[i] = 0;ans[c] =-1;}} else {DFS3 (tmp, delta); if (PD) return;}} inline void dfs1 (int tmp, int delta) {if (tmp > N) {if (delta = = 0) PD = 1;return;} int c = s1[tmp];if (ans[c] = = 1) {for (int i = 0; i < n; ++i) if (!vis[i]) {vis[i] = 1;ans[c] = I;DFS2 (tmp, delta); if (p d) Return;vis[i] = 0;ans[c] =-1;}} else {DFS2 (tmp, delta); if (PD) return;}} int main () {scanf ("%d\n", &n), char c;for (int i = n; i > 0; i) {c = GetChar (); s1[i] = C-' A ';} scanf ("\ n"); for (int i = n; i > 0; i) {c = GetChar (); s2[i] = C-' A ';} scanf ("\ n"); for (int i = n; i > 0; i) {c = GetChar (); an[i] = C-' A ';} for (int i = 0; i < n; ++i) ans[i] = -1;DFS1 (1, 0); for (int i = 0; i < n; ++i) printf ("%d", ans[i]); return 0;}

Positive solution: First sweep out the order of the letters appear, in this sweep out of the sequence of violence, after each violence simply sweep the whole equation to $check$, this is the only and most important I write the pruning, can get rid of the group of T-off data:

Input
-
nlhfieasbrqjogkmdpct
NQGPSIIGKDMFDCBFMQSO
Pnkntolheijhfgjkhjgg
Output
0 9 (7) 1 4 2 8 5 one 3 6

AC Code:

#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int s1[27], s2[27], an[27 ], ans[27], N, tb[27], TN = 0;bool vis[27], pan = 0;inline void mktb (int x) {if (!vis[x]) {vis[x] = 1;tb[++tn] = x;}} inline bool PD () {bool p = 0;int Delta = 0;for (int i = 1; I <= n; ++i) {int a = Ans[s1[i]], B = ans[s2[i]], C = ans[an[i ]];if (A = =-1 | | b = = 1) {p = 1;} else {if (c = = 1) {if (p) {if (a + b = = n-1) p = 1;elsep = 0, Delta = (A + b)/n;} el SE {p = 0;delta = (A + b + delta)/n;}}  else {if (p) {if ((A + b)% n = = c) {p = 0;delta = (A + b)/n;} else if ((A + B + 1)% n = = c) {p = 0;delta = (A + B + 1) /n;} Elsereturn 1;} else {if ((A + B + Delta)% n! = c) return 1;else {p = 0;delta = (A + b + delta)/n;}}}} if (!p && delta = = 1) return 1;elsereturn 0;} inline void dfs (int tmp) {if (PD ())) return;if (tmp > N) {pan = 1;return;}  for (int i = 0; i < n; ++i) if (!vis[i]) {vis[i] = 1;ans[tb[tmp]] = I;DFS (tmp + 1); if (pan) return;vis[i] = 0;ans[tb[tmp]] =-1;}} int main () {scanf ("%d\n", &n), char c;for (int i = n; i > 0; i) {c = GetChar (); s1[i] = C-' A ';} scanf ("\ n"); for (int i = n; i > 0; i) {c = GetChar (); s2[i] = C-' A ';} scanf ("\ n"); for (int i = n; i > 0; i) {c = GetChar (); an[i] = C-' A ';} for (int i = 1; I <= n; ++i) {MKTB (s1[i]); MKTB (S2[i]); MKTB (An[i]);} for (int i = 0; i < n; ++i) Vis[i] = 0, ans[i] = -1;dfs (1); for (int i = 0; i < n; ++i) printf ("%d", ans[i]); return 0;}

No,,,, XyX and I played a game of the Board of the afternoon, SG What's really not ah, the provincial election to explode 0 TWT

"NOIP 2004" Insect Food Calculation

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.