1. Trang chủ
  2. » Tất cả

/* Solves the problem using dynamic programming, time complexity: O ( item_nr * slot_nr ) (should be faster than sol2

8 2 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 8
Dung lượng 46 KB

Nội dung

/* Solves the problem using dynamic programming, time complexity O ( item nr * slot nr ) (should be faster than sol2 /* Solves the problem using dynamic programming, time complexity O ( item nr * slot[.]

/* Solves the problem using dynamic programming, time complexity: O ( item_nr * slot_nr ) (should be faster than sol2.c) */ #include #define MAX_SLOT_NR 101 #define MAX_ITEM_NR 101 /* the table weight keeps the weight of each item in each slot */ /* it will also be used to calculate the best (maximum) weights */ int weight[MAX_ITEM_NR][MAX_SLOT_NR]; /* the table index will be used to keep the solution(s) */ int f_index[MAX_ITEM_NR][MAX_SLOT_NR]; int slot_nr; /* the number of slots */ int item_nr; /* the number of items */ FILE *outf; void get_input(); void solve(); void print_solution(); void print_slots(int item, int slot); void print_table(int* table); void get_input() /* gets 'slot_nr', 'item_nr' from the file 'input.txt' */ /* and fills the table 'weight' with data from this file */ { int i,j; FILE *f = fopen("FLOWER.INP", "r"); fscanf(f, "%d %d\n", &item_nr, &slot_nr); for (i=0; i < item_nr; i++) for (j=0; j < slot_nr; j++) fscanf(f, "%d ", &(weight[i][j]) ); fclose(f); } void solve() /* calculates the maximum sum of weights for each item in each slot */ /* and also fills the 'index' table with info about how we obtained */ /* this maximum (needed to output the solutions) */ { int i, j, k; int wmax; int imax; for (i=1; i < item_nr; i++) { imax = i-1; wmax = weight[i-1][imax]; for (j=i; j wmax) { wmax = weight[i-1][j-1]; imax = j-1; /* record the index of maximum */ } weight[i][j] += wmax; f_index[i][j] = imax; } } } void print_solution() { int wmax; int imax; int j; /* take as max the first one */ imax = item_nr - 1; wmax = weight[item_nr-1][imax]; for (j=item_nr-1; j < slot_nr; j++) if ( weight[item_nr-1][j] > wmax ) { wmax = weight[item_nr-1][j]; imax = j; } fprintf(outf, "%d\n", weight[item_nr1][imax]); print_slots(item_nr-1, imax); } void print_slots(int item, int slot) /* postorder recursive function */ { int prev_slot; if (item == 0) fprintf(outf, "%d ", slot+1); else { prev_slot = f_index[item][slot]; print_slots(item-1, prev_slot); fprintf(outf, "%d ", slot+1); } } /* for debugging purposes void print_table(int* table) { int i, j; printf("\n %d %d \n\n", item_nr, slot_nr); for (i=0; i < item_nr; i++) { for (j=0; j < slot_nr; j++) printf(" %4d", *(table + i*MAX_SLOT_NR + j) ); printf("\n"); } }*/ int main() { get_input(); /* for debug printf("\n input table (weights): "); print_table((int*)weight); */ solve(); /* for debug printf("\n processed table (cumulative weights): "); print_table((int*)weight); printf("\n index table: "); print_table((int*)index); */ outf = fopen("FLOWER.OUT", "w"); print_solution(); fclose(outf); return 0; } 2/#include #include #include #include #define MAX_ST_ITEM 10000 #define MAX_CODE 100 #define MAX_LNGT 1000 #define MAX_CONC 1000 #define FC_NAME "words.inp" #define FI_NAME "text.inp" typedef struct st_struct { unsigned short code; long max, end, start; struct st_struct *prev, *link; } st_item; typedef struct ps_struct { unsigned short code, index; long start; struct ps_struct *next; } ps_item; st_item *head_PQ, *tail_PQ; ps_item *head_PS[256], *tail_PS[256]; char buf[1024], *code[MAX_CODE]; int curr_char; long pos; unsigned short CN, length[MAX_CODE], TableSize, *first[256], n_first[256]; unsigned long FileSize, n_ps_item, m_ps_item, n_st_item, NumberOfCodes; #ifndef TURBOC unsigned long min(unsigned long a, unsigned long b) { if (a < b) return a; else return b; } #endif void read_codes() { int i; unsigned char c; FILE *fc = fopen(FC_NAME, "r"); int dummy; fscanf(fc, " %d", &dummy); for (i = 0; i < dummy; i++) { fscanf(fc, "%s", buf); length[CN] = strlen(buf); code[CN] = strdup(buf); c = buf[0]; first[c] = realloc(first[c], sizeof(unsigned short) * (n_first[c] + 1)); first[c][n_first[c]++] = CN++; } fclose(fc); } void insert(unsigned short curr_char, unsigned short code, unsigned short index, long start) { ps_item *p = (ps_item *)malloc(sizeof(ps_item)); p->code = code, p->index = index, p->start = start; p->next = NULL; if (tail_PS[curr_char]) tail_PS[curr_char]->next = p; else head_PS[curr_char] = p; tail_PS[curr_char] = p; if (++n_ps_item > m_ps_item) m_ps_item = n_ps_item; } void insert_all(long i) { int j; unsigned short *p = first[curr_char]; for (j = 0; j < n_first[curr_char]; j++) insert(curr_char, p[j], 0, i); } void delete_item(ps_item *prev, ps_item *curr) { if (prev == NULL) head_PS[curr_char] = curr->next; else prev->next = curr->next; if (curr == tail_PS[curr_char]) tail_PS[curr_char] = prev; free(curr); n_ps_item; } void create_link(st_item *s) { st_item *q = tail_PQ; while (q) { if (q->end < s->start) { s->link = q, s->max = q->max + length[s->code]; return; } q = q->prev; } s>link = NULL, s->max = length[s->code]; } void insert_priq(st_item *s) { st_item *q = tail_PQ, *prev = NULL; while (q) { if (q->max > s->max) { prev = q; q = q->prev; } else { _found: s->prev = q; if (prev) prev->prev = s; else tail_PQ = s; return; } } goto _found; } void _found_item(ps_item *p, long end) { st_item *s = (st_item *)malloc(sizeof(st_item)); s->code = p->code, s->start = p->start, s->end = end; create_link(s); insert_priq(s); ++n_st_item; } void search_all(long i) { unsigned char c; ps_item *p = head_PS[curr_char], *prev = NULL, *curr; while (p) { if ((i - p->start) > MAX_LNGT) { curr = p; goto remove; } c = code[p->code][++p->index]; curr = p; if (c == 0) { _found_item(p, i); } else if (c != curr_char) { insert(c, p->code, p->index, p>start); } else goto do_not_remove; remove: p = p->next; delete_item(prev, curr); goto _next; do_not_remove: prev = p; p = p->next; _next: ; } } void delete_all() { int i; ps_item *curr, *head; for (i = 0; i < 256; i++) if (head = head_PS[i]) { while (head) { curr = head; head = head->next; free(curr); } tail_PS[i] = NULL; } } int search() { int retval = 0; FILE *fi = fopen(FI_NAME, "r"); pos = 0; n_st_item = n_ps_item = m_ps_item = 0; while ((curr_char = fgetc(fi)) != EOF) { insert_all(pos); search_all(pos++); if (n_st_item >= NumberOfCodes) { retval = 1; break; } } fclose(fi), delete_all(); return retval; } void output() { FILE *fo = fopen("codes.out", "w"); fprintf(fo, "%ld\n", tail_PQ->max); while (tail_PQ) { fprintf(fo, "%d %ld %ld\n", tail_PQ->code + 1, tail_PQ->start + 1, tail_PQ>end + 1); tail_PQ = tail_PQ->link; } fclose(fo); } void main(int argc, char *argv[]) { NumberOfCodes = MAX_ST_ITEM; read_codes(); search(); output(); } 3/program underground; uses undertpu; const wall = 'W'; opensqr = 'O'; marker = 'c'; possible = 'P'; impossible = 'I'; unknown = 'U'; max = 99; max2 = 66*66; type labi = array [0 max,0 max] of char; var f:TEXT; n,m:integer; board,situation,candidate:labi; wallcount : array [0 max,0 max]of integer; x,y:integer; flag:boolean; questionx,questiony:integer; questionch:char; answer:char; path:array [1 max2] of char; candidate_number:integer; procedure read_data; var i,j:integer; begin candidate_number:=0; assign(f,'under.inp'); reset(f); readln(f,m,n); for i:=0 to n-1 begin for j:=0 to m-1 begin read(f,board[i,j]); if (board[i,j]=opensqr) then begin candidate[i,j]:=possible;inc(candidate_number);end else candidate[i,j]:=impossible; situation[i,j]:=unknown; end; readln(f); end; situation[0,0]:=opensqr; close(f); end; function onboard(a,b,i,j:integer):char; begin onboard:=board[(a+i) mod n, (b+j) mod m];end; function up(x:integer):integer; begin up:=(((x)+n-1) mod n);end; function left(x:integer):integer; begin left:=(((x)+m-1) mod m);end; function down(x:integer):integer; begin down:=(((x)+1) mod n);end; function right(x:integer):integer; begin right:=(((x)+1) mod m);end; procedure minus_one_to_wallcount; var i,j:integer; begin for i:=0 to n-1 for j:=0 to m-1 wallcount[i,j]:=-1; end; procedure count_walls(a,b:integer); var i,j:integer; begin flag:=true; wallcount[a,b]:=0; for i:=0 to n-1 for j:=0 to m-1 if (candidate[i,j]=possible) and (onboard(i,j,a,b)=wall) then wallcount[a,b]:=wallcount[a,b] +1; end; procedure find_question_place(a,b:byte); begin if(situation[a,b]=unknown) then begin count_walls(a,b); if(wallcount[a,b]=0)then begin situation[a,b]:=opensqr;wallcount[a,b]:=-1;end else if(wallcount[a,b]=candidate_number) then begin situation[a,b]:=wall;wallcount[a,b]:=-1;end; end; if(situation[a,b]=opensqr) then begin situation[a,b]:=marker; find_question_place(a,right(b)); find_question_place(a,left(b)); find_question_place(up(a),b); find_question_place(down(a),b); end; end; procedure clean_markers; var i,j:integer; begin for i:=0 to n-1 for j:=0 to m-1 if(situation[i,j]=marker) then situation[i,j]:=opensqr; end; function where_to_ask:boolean; var i,j:integer; su_anki_oran : integer; begin flag:=false; su_anki_oran:=candidate_number; minus_one_to_wallcount; find_question_place(0,0); clean_markers; if(flag) then begin for i:=0 to n-1 for j:=0 to m-1 if(wallcount[i,j] > -1) then begin if (abs(wallcount[i,j]-candidate_number div 2)max2) then begin writeln('Kafayi mi yedin? max2''yi gectin');halt;end; path[adim]:='E';find_the_path(a,right(b),adim+1); if(not I_found_the_path) then begin path[adim]:='W';find_the_path(a,left(b),adim+1);end; if(not I_found_the_path) then begin path[adim]:='N';find_the_path(up(a),b,adim+1);end; if(not I_found_the_path) then begin path[adim]:='S';find_the_path(down(a),b,adim+1);end; end; end; end; procedure move_to; var i:integer; begin for i:=1 to top move(path[i]); x:=questionx; y:=questiony; end; begin I_found_the_path:=false; find_the_path(x,y,1); clean_markers; move_to; answer:=look(questionch); end; procedure decrease_candidates; var i,j:integer; begin case questionch of 'E':questiony:=right(questiony); 'W':questiony:=left(questiony); 'N':questionx:=up(questionx); 'S':questionx:=down(questionx); end; situation[questionx,questiony]:=answer; for i:=0 to n-1 for j:=0 to m-1 if (candidate[i,j]=possible) then if (board[(i+questionx) mod n,(j+questiony) mod m]answer) then begin candidate[i,j]:=impossible; dec(candidate_number); end; end; procedure write_the_answer; var i,j:integer; begin for i:=0 to n-1 for j:=0 to m-1 if candidate[i,j]=possible then begin writeln('answer: ','(',j+1,',',n-i,')'); finish(j+1,n-i); end; end; begin start; read_data; while((candidate_number>1) and where_to_ask) begin ask_the_question; decrease_candidates; end; if(candidate_number>1) then writeln('Attention ! More than one solution') else if (candidate_number=0) then writeln('Attention ! No answer'); write_the_answer; end unit undertpu; interface procedure start; procedure move(ch:char); function look(ch:char):char; procedure finish(x,y:integer); implementation uses dos; const serdarWALL='W'; serdarOPEN='O'; serdarMARK='c'; OUR_FILE_SIZE =6; start_not_called = 1; move_to_wall = 2; file_size_wrong = 3; wrong_argument = 4; a_lot_start = 5; a_lot_looks = 7; var serdarn,serdarm :integer; serdartahta:array [0 99,0 99] of char; serdarch :char; serdarx,serdary,serdarmin :integer; basx,basy :integer; serdarcompetition :boolean; serdar_num_looks :word; serdarstart :boolean; ffblk:SearchRec; our_file:string[10]; eval_inp_dosyasi :string[10]; inp_dosyasi :string[11]; pla_dosyasi :string[10]; xy_dosyasi :string[7]; puan_dosyasi :string[9]; result_dosyasi:string[11]; ten,zero:byte; procedure serdar_error(x:integer);forward; function serdarust(x:integer):integer; begin serdarust:=(((x)+serdarn-1) mod serdarn);end; function serdarsol(x:integer):integer; begin serdarsol:=(((x)+serdarm-1) mod serdarm);end; function serdaralt(x:integer):integer; begin serdaralt:=(((x)+1) mod serdarn);end; function serdarsag(x:integer):integer; begin serdarsag:=(((x)+1) mod serdarm);end; procedure solve_names; var i:integer; begin for i:=1 to Length(inp_dosyasi) inp_dosyasi[i]:=chr(ord(inp_dosyasi[i])+40); for i:=1 to Length(eval_inp_dosyasi) eval_inp_dosyasi[i]:=chr(ord(eval_inp_dosyasi[i])+40); for i:=1 to Length(pla_dosyasi) pla_dosyasi[i]:=chr(ord(pla_dosyasi[i])+40); for i:=1 to Length(xy_dosyasi) xy_dosyasi[i]:=chr(ord(xy_dosyasi[i])+40); for i:=1 to Length(our_file) our_file[i]:=chr(ord(our_file[i])+40); for i:=1 to Length(puan_dosyasi) puan_dosyasi[i]:=chr(ord(puan_dosyasi[i])+40); for i:=1 to Length(result_dosyasi) result_dosyasi[i]:=chr(ord(result_dosyasi[i])+40); end; procedure start; var i,j:integer; f:TEXT; begin if (serdarstart) then serdar_error(a_lot_start); solve_names; findfirst(our_file,AnyFile,ffblk); if(doserror0) then serdarcompetition:=true else begin if(ffblk.size = OUR_FILE_SIZE) then serdarcompetition:=False else begin serdarstart:=true; serdar_error(file_size_wrong); end; end; if(serdarcompetition) then assign(f,inp_dosyasi) else assign(f,eval_inp_dosyasi); reset(f); read(f,serdarm,serdarn); for i:=0 to serdarn-1 begin for j:=0 to serdarm-1 read(f,serdartahta[i,j]); readln(f); end; read(f,serdarch); close(f); if(serdarcompetition) then begin assign(f,pla_dosyasi); reset(f); read(f,serdarx,serdary); close(f); end else begin assign(f,xy_dosyasi); reset(f); read(f,serdarx,serdarmin,serdary); close(f); end; basx:=serdarx; basy:=serdary; serdary:=basx-1; serdarx:=serdarn-basy; serdarstart:=true; end; procedure serdar_error(x:integer); var f:TEXT; begin if(not serdarstart) then solve_names; if(serdarcompetition) then begin assign(f,result_dosyasi); rewrite(f); write(f,'Error'); close(f); end else begin assign(f,puan_dosyasi); rewrite(f); write(f,x,' ',zero); close(f); end; halt; end; procedure move(ch:char); begin if(not serdarstart)then serdar_error(start_not_called); case ch of 'S':serdarx:=serdaralt(serdarx); 'N':serdarx:=serdarust(serdarx); 'E':serdary:=serdarsag(serdary); 'W':serdary:=serdarsol(serdary); else serdar_error(wrong_argument); end; if(serdartahta[serdarx,serdary]serdarOPEN)then serdar_error(move_to_wall); end; function look(ch:char):char; begin if(not serdarstart)then serdar_error(start_not_called); if (serdar_num_looks>20000) then serdar_error(a_lot_looks); inc(serdar_num_looks); case ch of 'S':if(serdartahta[serdaralt(serdarx),serdary]=serdarOPEN) then look:=serdarOPEN else look:=serdarWALL; 'N':if(serdartahta[serdarust(serdarx),serdary]=serdarOPEN) then look:=serdarOPEN else look:=serdarWALL; 'E':if(serdartahta[serdarx,serdarsag(serdary)]=serdarOPEN) then look:=serdarOPEN else look:=serdarWALL; 'W':if(serdartahta[serdarx,serdarsol(serdary)]=serdarOPEN) then look:=serdarOPEN else look:=serdarWALL; else serdar_error(wrong_argument); end; end; procedure finish(x,y:integer); var f:TEXT; puan:integer; begin if(not serdarstart)then serdar_error(start_not_called); if (serdarcompetition) then assign(f,result_dosyasi) else assign(f,puan_dosyasi); rewrite(f); if(serdarcompetition) then begin writeln(f,x,' ',y); write(f,'You used look ',serdar_num_looks,' times'); end else if(x=basx) and (y=basy) then begin if (serdar_num_looks = serdarmin+serdarmin) then puan:=zero else puan:= trunc ( ( 10.0 * ( 2.0 * serdarmin serdar_num_looks ) ) / serdarmin + 0.5); write(f,zero,' ',puan); end else begin write(f,ten,' ',zero); end; close(f); halt; end; begin serdar_num_looks:=0; serdarstart:=false; ten:=10; zero:=0; eval_inp_dosyasi[0]:=chr(9); eval_inp_dosyasi[1]:=chr(65); eval_inp_dosyasi[2]:=chr(70); eval_inp_dosyasi[3]:=chr(72); eval_inp_dosyasi[4]:=chr(77); eval_inp_dosyasi[5]:=chr(76); eval_inp_dosyasi[6]:=chr(6); eval_inp_dosyasi[7]:=chr(76); eval_inp_dosyasi[8]:=chr(80); eval_inp_dosyasi[9]:=chr(76); eval_inp_dosyasi[10]:=chr(0); inp_dosyasi[0]:=chr(9); inp_dosyasi[1]:=chr(77); inp_dosyasi[2]:=chr(70); inp_dosyasi[3]:=chr(60); inp_dosyasi[4]:=chr(61); inp_dosyasi[5]:=chr(74); inp_dosyasi[6]:=chr(6); inp_dosyasi[7]:=chr(65); inp_dosyasi[8]:=chr(70); inp_dosyasi[9]:=chr(72); inp_dosyasi[10]:=chr(0); pla_dosyasi[0]:=chr(9); pla_dosyasi[1]:=chr(72); pla_dosyasi[2]:=chr(68); pla_dosyasi[3]:=chr(57); pla_dosyasi[4]:=chr(59); pla_dosyasi[5]:=chr(61); pla_dosyasi[6]:=chr(6) ; pla_dosyasi[7]:=chr(76); pla_dosyasi[8]:=chr(80); pla_dosyasi[9]:=chr(76); pla_dosyasi[10]:=chr(0) ; xy_dosyasi[0]:=chr(6); xy_dosyasi[1]:=chr(80); xy_dosyasi[2]:=chr(81); xy_dosyasi[3]:=chr(6) ; xy_dosyasi[4]:=chr(63); xy_dosyasi[5]:=chr(65); xy_dosyasi[6]:=chr(74); xy_dosyasi[7]:=chr(0) ; puan_dosyasi[0]:=chr(8); puan_dosyasi[1]:=chr(72); puan_dosyasi[2]:=chr(77); puan_dosyasi[3]:=chr(57); puan_dosyasi[4]:=chr(70); puan_dosyasi[5]:=chr(6) ; puan_dosyasi[6]:=chr(59); puan_dosyasi[7]:=chr(65); puan_dosyasi[8]:=chr(67); puan_dosyasi[9]:=chr(0) ; result_dosyasi[0]:=chr(10); result_dosyasi[1]:=chr(74); result_dosyasi[2]:=chr(61); result_dosyasi[3]:=chr(75); result_dosyasi[4]:=chr(77); result_dosyasi[5]:=chr(68); result_dosyasi[6]:=chr(76); result_dosyasi[7]:=chr(6); result_dosyasi[8]:=chr(76); result_dosyasi[9]:=chr(80); result_dosyasi[10]:=chr(76); result_dosyasi[11]:=chr(0); our_file[0]:=chr(9); our_file[1]:=chr(67); our_file[2]:=chr(61); our_file[3]:=chr(68); our_file[4]:=chr(57); our_file[5]:=chr(68); our_file[6]:=chr(57); our_file[7]:=chr(67); our_file[8]:=chr(57); our_file[9]:=chr(6) ; our_file[10]:=chr(0) ; end 4/#include "faruk.h" #ifndef INT_MAX #define INT_MAX 2147483647 #endif #define unseen (INT_MAX - 2) #define BLUE #define PURPLE FILE *ifp, *ofp; /************************************** init declarations ***************/ struct node { short v ; /* name of the vertex */ short w ; /* weight */ struct node *next; }; short V, E; /* number of Vertices and Edges * * V v=y; t->w=weight; t->next=adj[x]; adj[x]=t; } } } void get_vertices() { char state; register short i; for (i = 1; i v] < 0) if (f_update(t->v, prcalc(k,t->v,val[k]) + val[k] + t->w)) { val[t>v] = -(prcalc(k,t->v,val[k]) + val[k] + t->w); dad[t->v] = k; } } } void allsp() { register short k; f_initialize(); for (k = 1; k v[M - 1]) && v[M - 1])) if (a[M] > max) return M; return ent; } void flatten() { register short i; short value, entry, no_chip, temp; FILE *sol; while (nze > 0) { value = 0; nfp = 0; for (i = 1; i = v[i]) { /* hepsine basilabileni sec */ entry = i; no_chip = v[i]; nze; nfp = 1; break; } if (eval(i) > value) { /* eger fazla basilabilecek varsa */ value = eval(i); entry = i; no_chip = MPC(i); } if (eval(i) == value) if (eval2(i) > eval2(entry)) { entry = i; no_chip = MPC(i); } } } if (!nfp) if ((temp = find_2nd())) { entry = temp; no_chip = MPC(entry); } v[entry] -= no_chip; a[entry] -= no_chip; if (entry == M) { a[entry - 1] += no_chip; write_it(entry,no_chip);} else if (entry == 1) { a[2] += no_chip; write_it(entry,no_chip);} else { a[entry - 1] += (no_chip / 2); a[entry + 1] += (no_chip / 2); write_it(entry,no_chip / 2); } } sol = f_fopen("SOLUTION.TXT","w"); fprintf(sol,"%hd\n", step); printf("%hd\n", step); fclose(sol); } void putitout() { short i; FILE *ofp; ofp = f_fopen("FLAT.OUT","w"); fprintf(ofp,"%hd\n", step); for (i = 1; i

Ngày đăng: 31/12/2022, 15:55

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w