Click to open link
This is a string simulation, test instructions is probably given a m*n grid, Haig with ' * ', the white lattice has a letter, if a white lattice left or above the black lattice, it is called a starting lattice.
Then find all the horizontal and vertical words, note that the place of the horizontal word refers to start from a beginning to the right or down, until the black lattice or out of bounds, and each letter in the search for horizontal or vertical words can only use a
Times, so divided into two steps, horizontal and vertical search, each time to determine whether the lattice is the beginning of the grid, and if in the previous word used, if not used to find down, using the past will continue to find the starting grid.
#include <iostream> #include <cstdio> #include <string.h> #include <map> #include <stack> #include <queue> #include <algorithm> #include <math.h> #include <vector> #include <set># Define from (I,a,n) for (int. i=a;i<n;i++) #define REFROM (i,n,a) for (int i=n;i>=a;i--) #define EPS 1e-10#define MoD 100 0000007using namespace Std;const double inf=0x3f3f3f3f;const int MAX =11;char puzzle[max][max],record[max][max];// The record array here is used to log whether the starting lattice has been used to make the word int r,c,pos;void across () {int cnt=0; memset (record,0,sizeof (record)); printf ("across\n"); From (I,0,r) {from (J,0,C)//From left to right, from top to bottom scan {int y=j; if (i==0&&puzzle[i][j]!= ' * ')//first line all is not black lattice is the beginning of the lattice {cnt++; if (!record[i][j]) printf ("%3d.", CNT);//If the starting cell is not used, you can find a word else continue; while (puzzle[i][y]!= ' * ' &&y<c)//to the right output, until you meet the black lattice or cross-border {if (!recOrd[i][y]) {printf ("%c", Puzzle[i][y]); Record[i][y++]=1; } else break; } printf ("\ n"); Continue if (j==0&&puzzle[i][j]!= ' * ')//The first column is not a black lattice but the starting cell {cnt++; if (!record[i][j]) printf ("%3d.", CNT); else continue; while (puzzle[i][y]!= ' * ' &&y<c) {if (!record[i][y]) { printf ("%c", Puzzle[i][y]); Record[i][y++]=1; } else break; } printf ("\ n"); Continue } if (puzzle[i][j]!= ' * ' && (puzzle[i][j-1]== ' * ' | | puzzle[i-1][j]== ' * '))//is not a black lattice and the left or upper is a black lattice {cnt++; if (!record[i][j]) printf ("%3d.", CNT); else continue; while (puzzle[i][y]!= ' * ' &&y<c) {if (!record[i][y]) { printf ("%c", Puzzle[i][y]); Record[i][y++]=1; } else break; } printf ("\ n"); Continue }}} return;} void down () {int cnt=0; memset (record,0,sizeof (record)); printf ("down\n"); From (I,0,r) {from (j,0,c) {int x=i; if (i==0&&puzzle[i][j]!= ' * ') {cnt++; if (!record[i][j]) printf ("%3d.", CNT); else continue; while (puzzle[x][j]!= ' * ' &&x<r) {if (!record[x][j]) { printf ("%c", Puzzle[x][j]); Record[x++][j]=1; } else break; } printf ("\ n"); Continue } if (j==0&&puzzle[i][j]!= ' * ') {cnt++; if (!record[i][j]) printf ("%3d.", CNT); else continue; while (puzzle[x][j]!= ' * ' &&x<r) {if (!record[x][j]) { printf ("%c", Puzzle[x][j]); Record[x++][j]=1; } else break; } printf ("\ n"); Continue } if (puzzle[i][j]!= ' * ' && (puzzle[i][j-1]== ' * ' | | puzzle[i-1][j]== ' * ')) {cnt++; if (!record[i][j]) printf ("%3d.", CNT); else continue; while (puzzle[x][j]!= ' * ' &&x<r) {if (!record[x][j]) { printf ("%c", Puzzle[x][j]); Record[x++][j]=1; } else break; } printf ("\ n"); Continue }}} return;} int main () {int t=0; while (Cin>>r) {if (r==0) break; cin>>c; GetChar (); From (I,0,r) gets (Puzzle[i]); if (t) printf ("\ n"); printf ("Puzzle #%d:\n", ++t); Across ();//horizontal find down ();//Vertical Find} return 0;}
The specific code is as follows:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
uva--232 (string emulation)