Codeforces 487D. Conveyor Belts sub-block +DP

Source: Internet
Author: User


Test instructions

There is a NXM map with three symbols on the top, left, right, respectively.

There are two actions, a X y asks which point or loop to go from one point (x, y), and C x y ch to change the symbol (x, y) of the map to Ch


Given that there is no downward action, there is only one possibility of being trapped in a dead loop: ' > ' < '

Block operations, divided into sqrt (n) blocks, with DP preprocessing points in each block can be to which point, 1 means the point of the dead loop

For operation A, if the point is moved to the boundary of the block at which it is located, it is output. Otherwise the output of the recursion is the upper piece

For Operation C, re-click on the block of the DP, because C operation up to only 1W times, and the block processing, so not too slow ....



D. Conveyor Beltstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Automatic Bakery of Cyberland (ABC) recently bought an nx? M Rectangle table. To serve the diners, ABC placed seats around the table. The size of each seat was equal to a unit square, so there is 2 (n? +? M) seats in total.

ABC placed conveyor belts on each unit square on the table. There is three types of conveyor belts: "^", "<"and">". A^"Belt can bring things upwards."<"Can bring Leftwards and">"Can bring rightwards.

Let's number the rows with1ToNFrom top to bottom, the columns with1TomFrom left to right. We consider the seats above and below the top of the table are rows0and n? +?1respectively. Also we define seats to the left of the table and to the-the table to is column0and m? +?1. Due to the conveyor belts direction restriction There is currently no-in-a diner sitting in the row n? +?1To is served.

Given The initial table, there'll be is Q events in order. There is types of events:

  • "Ax y"Means, a piece of bread would appear at rowxand columny(we'll denote such position as(x,? Y)). The bread would follow the conveyor belt, until arriving at a seat of a diner. It is possible, the bread gets stuck in an infinite loop. Your task is to simulate the process, and output the final position of the bread, or determine that there would be a infin ITE Loop.
  • "C x y c" means the type of the conveyor belt at (x,? Y) is changed to C.

Queries is performed separately meaning that even if the bread got stuck in an infinite loop, it won ' t affect further que Ries.

Input

The first line of input contains three integers n, m and Q (1?≤? N? ≤?105,? 1?≤? m? ≤?10,?1?≤? q. ≤?105), separated by a space.

Next n lines, each line contains m characters, describing the table. The characters can only be one of the "<^>".

NextQLines, each of the describes an event. The format is "c x y c"or"A x y"(consecutive elements is separated by a space). It ' s guaranteed that1?≤? x? ≤? n,? 1?≤? y? ≤? m .Cis a character from the set "<^>".

There is at the most 10000 queries of "C" type.

Output

For Each event of type "A", Output of Integers  TX ,   ( x ,? y )  is  ( Tx ,? ty ) .

If there is an infinite loop, do you should output tx? =? Ty? =?? -?1.

Sample Test (s) input
2 2 3>> ^^ A 2 1C 1 2 <a 2 1
Output
1 3-1-1
Input
4 5 7><<^<^< ^^ >>>>^>>^>>^a 3 1 a 2 2C 1 4 <a 3 1C 1 2 ^a 3 1 A 2 2
Output
0 4-1-1-1-10 20 2
Note

For the first sample:

If the bread goes from (2,?1), it'll go out of the table at (1,?3).

After changing the conveyor belt of (1,?2) to '<', when the bread goes from (2,?1) again, it Would get stuck at '><', so output is (?-? 1,?? -?1).



#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include < algorithm>using namespace Std;const int maxn=100100;int n,m,q;int Kuai,kn;char str[maxn][12];int dp[maxn][12]; Dp[y][z]=w: In the X block, the z column of the y row goes to the W lattice int dfs (int k,int x,int y) {if (Dp[x][y]) return dp[x][y];if (str[x][y]== ' ^ ') {int nx=x-1 , Ny=y;if (nx<1+ (k-1) *kuai) {dp[x][y]=nx* (m+2) +ny;} Else{dp[x][y]=dfs (K,nx,ny);}} else if (str[x][y]== ' > ') {int nx=x,ny=y+1;if (ny==m+1) {dp[x][y]=nx* (m+2) +ny;} else if (str[nx][ny]== ' < ') {dp[x][y]=-1;dp[nx][ny]=-1;} else {Dp[x][y]=dfs (k,nx,ny);}} else if (str[x][y]== ' < ') {int nx=x,ny=y-1;if (ny==0) {dp[x][y]=nx* (m+2) +ny;} else if (str[nx][ny]== ' > ') {dp[x][y]=-1;dp[nx][ny]=-1;} else {Dp[x][y]=dfs (k,nx,ny);}} return dp[x][y];} In the first few blocks void GetPOS (int x) {//Range of Row//1+ (x-1) *kuai ~ x*kuaifor (int r=1+ (x-1) *kuai;r<=min (x*kuai,n); r++) {for (int c=1;c<=m;c++) {DFS (X,R,C);}}} void Changeit (int k,int x,int Y,char c) {for (int r=1+ (k-1) *kuai;r<=min (K*kuai,n); r++) for (int c=1;c<=m;c++) Dp[r][c]=0;str[x][y]=c;getpos (k);} int FindIt (int k,int x,int y) {if (dp[x][y]==-1) return-1;if (k==1) return dp[x][y];int temp=dp[x][y];int nx=temp/(m+2); in T ny=temp% (m+2); if (ny!=0&&ny!=m+1) return FindIt (k-1,nx,ny); return temp;} int main () {scanf ("%d%d%d", &n,&m,&q), for (int i=1;i<=n;i++) scanf ("%s", str[i]+1), Kuai=int (sqrt (n)) +1 ; Kn=n/kuai;if (N%kuai) kn++;for (int i=1;i<=kn;i++) GetPOS (i), Char cmd[20],ch[10];int x,y;while (q--) {scanf ("%s", CMD), if (cmd[0]== ' A ') {scanf ("%d%d", &x,&y), int nn= (X-1)/kuai+1;int ID = FindIt (nn,x,y), if (id>=0) printf ("% D%d\n ", ID (m+2), id% (m+2)); else puts ("-1-1 ");} else if (cmd[0]== ' C ') {scanf ("%d%d%s", &x,&y,ch); int nn= (X-1)/kuai+1;changeit (nn,x,y,ch[0]);}} return 0;}





Codeforces 487D. Conveyor Belts sub-block +DP

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.