UVa 196 spreadsheet

Source: Internet
Author: User

Test instructions: Given some spreadsheets, each cell is either an integer or a formula, the formula is the = sign, and the string with the + sign is concatenated. The values of the other cells are used in the formula, and the other cells that you want to use may be a formula. However, it is guaranteed that no circular reference to the cell will be formed. Where the line's label range is from 1 to 999, and the column's label range is from a to zzz. Requires that the formula be replaced with a numeric value at the time of output.

Idea: It's easy to see that this is related to topological ordering, and the calculation of one cell value must be done before the other. But there is no need to sort the topology here.  DFS only needs to be recursive at the time of DFS access. The middle of the error, such as the comment section of the code content. The second error is particularly serious, because STRCHR (str, CH), if after sp=p, the parameter is not sp+1, but the SP, the incoming parameter is equivalent to an empty string ~

Summary: 1. Takes the contents of the string. such as "=a1+a2+b3+c4" in the A1, A2 and other content, such as 88-97 lines of code.

2. Conversion of the binary. Converts a, B 、...、 zzz to integers, code such as the Alphatoint function of 106 lines, with emphasis on the concise code in the For loop.

Code:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>int dfs (char* sp); int Alphatoint (char *r), void solve (int row,int col), char* Sheet[1000][18300];char str[100];int main () {//freopen ("196.  In "," R ", stdin);    Freopen ("196.out", "w", stdout);  int num;  scanf ("%d", &num);    while (num-->0) {int col,row;    scanf ("%d%d", &col,&row);       for (int i=1;i<=row;++i) for (int j=1;j<=col;++j) {scanf ("%s", str);       char* c= (char*) malloc (strlen (str) +1);       if (c==null) printf ("false\n");       Sheet[i][j]=c;     strcpy (SHEET[I][J],STR);    } solve (Row,col);            Print for (int i=1;i<=row;++i) {for (int j=1;j<col;++j) {printf ("%s", Sheet[i][j]);     }//printf ("%s\n", Sheet[i][col-1]);    printf ("%s\n", Sheet[i][col]); }//free-pointer for (int i=1;i<=row;++i) for (int. j=1;j<=col;++j) {if (sheet[i][j]!=null) {free ( SHEET[I][J]);     Sheet[i][j]=null;}      }} return 0;}         void solve (int row,int col) {for (Int. i=1;i<=row;++i) for (int j=1;j<=col;++j) {DFS (sheet[i][j]);              }}int dfs (char* sp) {if (sp[0]!= ' = ') {//is not a formula if (IsDigit (Sp[0])) {return atoi (SP);      The cell name {int n=0, A1, A2, and so on when the else//is called recursively;      while (Isalpha (sp[n))) n++;      Char c[4];      strncpy (C,sp,n);      c[n]= ' + ';      Char r[4];      strcpy (R,sp+n);      int Co=alphatoint (c);      int Ro=atoi (R);    Return Dfs (Sheet[ro][co]);  }} char* dp=sp;  char* ep= (char*) malloc (strlen (SP) +1-1);  int cnt=0;  char* P=STRCHR (sp, ' + ');    while (p!=null) {*p= ';    strcpy (ep,sp+1);    Cnt=cnt+dfs (EP);    Sp=p;            P=STRCHR (sp, ' + ');  P=STRCHR (sp+1, ' + ');  } strcpy (ep,sp+1);  Cnt=cnt+dfs (EP);  sprintf (DP, "%d", CNT);  Free (EP);  Ep=null; return CNT;}  int Alphatoint (char *r) {int cnt=0;  int Len=strlen (R);  for (int i=0;i<len;++i) {cnt=cnt*26+ (r[i]-' A ' + 1); } return CNT;}


UVa 196 spreadsheet

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.