Thursday, March 31, 2016

UVa 12375 - Truchet Tiling







The tricky part of the solution is to realize how the color is flowing from cell to cell (regions).
We have two types of motifs 0 and 1. I have divided the motifs into 4 regions. In motif - 0 we have
regions 1,2,3,4 and in motif - 1 we have regions 5,6,7,8.
So,we will go from cell to cell and add the major region's area to solution and mark that region visited.
For region 1,4,6,7 we will add PI/4 (Pi*(r^2)/4) and for other regions we will add (4 - PI/2)/2 to
solution.
And about the movement , look at the figure we can't go to any cell from a certain cell
.For example,we can only go upward and left from region 1.Once We are in some cell if it's not
visited (if it's visited we have already added it's area to solution) and it's a valid cell(not out of the boundary) then we add that cell's/region's area to the
solution.

Code : -


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/****************************************

Cat Got Bored

*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<e;i++) //excluding end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d:\n",++t) // t initialized 0

#define INF 9999999999999   //10e9

#define EPS 1e-9


using namespace std;

//Graph Construction
 int rowMove[9][3];
 int colMove[9][3];
const int callRM[] = {0,0,1,1};
const int callCM[] = {0,1,0,1};
int RR,CC;
int graph[205][205];
bool visited[205][205];

void graph_init()
{

    rowMove[1][0] = 0; rowMove[1][1] = -1;rowMove[1][2] = 0;
    colMove[1][0] =-1; colMove[1][1] =0; colMove[1][2] =0;
    rowMove[2][0] = 0 ;rowMove[2][1] = -1 ;rowMove[2][2] = 1 ;
    colMove[2][0] = 1 ; colMove[2][1] = 0 ; colMove[2][2] = -1 ;
    rowMove[3][0] = 0 ;rowMove[3][1] = 1 ;rowMove[3][2] = -1 ;
    colMove[3][0] = -1;colMove[3][1] = 0;colMove[3][2] = 1;
    rowMove[4][0] = 0 ;rowMove[4][1] = 1 ;rowMove[4][2] = 0 ;
    colMove[4][0] =1;colMove[4][1] =0;colMove[4][2] =0;
    rowMove[5][0] = 0;rowMove[5][1] = -1;rowMove[5][2] = 1;
    colMove[5][0] = -1;colMove[5][1] = 0;colMove[5][2] = 1;
    rowMove[6][0] = 0;rowMove[6][1] = -1;rowMove[6][2] = 0;
    colMove[6][0] =1;colMove[6][1] =0;colMove[6][2] =0;
    rowMove[7][0] =0;rowMove[7][1] =1;rowMove[7][2] =0;
    colMove[7][0] = -1;colMove[7][1] = 0;colMove[7][2] = 0;
    rowMove[8][0] = 0;rowMove[8][1] = 1;rowMove[8][2] = -1;
    colMove[8][0] = 1;colMove[8][1] = 0;colMove[8][2] = -1;


}
bool valid(int r,int c)
{
    if(r<1 || r>2*RR || c<1 || c>2*CC)
        return false;
    return true;
}
double area_grid(int r,int c)
{
    int grid_no = graph[r][c];
    int g_n = grid_no;
    if(g_n == 1 || g_n == 4 || g_n == 6 || g_n == 7 )
        return ((PI)/4.00000)*1.00000;
    else
        return (4.0000000 - (PI/2.000000)*1.000000)/2.0000000;
}
double area(int r,int c)
{
    if(visited[r][c])
        return 0.00000000;
    if(!valid(r,c))
        return 0.00000000;

    visited[r][c] = true;
    double partial_area = 0.00000000;
    for(int i=0; i<3; i++)
    {
        int r_pp = r + rowMove[  graph[r][c]   ][i];
        int c_pp = c + colMove[  graph[r][c]   ][i];
        partial_area += area(r_pp,c_pp);
    }
    return area_grid(r,c) + partial_area ;
}


int main()
{
    int tc;
    cin>>tc;
    int cas = 0;
    graph_init();
    while(tc--)
    {
        cin>>RR>>CC;
        int rrr = 1 ;
        for(int r = 0; r<RR; r++)
        {
            string sss;
            cin>>sss;
            int ccc = 1 ;
            for(int c = 0; c<CC; c++)
            {
                if(sss[c]=='0')
                {
                    int r_p = 2*r + 1;
                    int c_p = 2*c + 1;
                    graph[r_p][c_p] = 1;
                    graph[r_p][c_p+1] = 2;
                    graph[r_p+1][c_p] = 3;
                    graph[r_p+1][c_p+1] = 4;
                }
                if(sss[c]=='1')
                {
                    int r_p = 2*r + 1;
                    int c_p = 2*c + 1;
                    graph[r_p][c_p] = 5;
                    graph[r_p][c_p+1] = 6;
                    graph[r_p+1][c_p] = 7;
                    graph[r_p+1][c_p+1] = 8;
                }


            }


        }
/*
        int i,j;
        loop(i,1,2*RR+1)
        {
            loop(j,1,2*CC+1)
            {
                cout<<graph[i][j];
            }
            cout<<endl;
        }
*/

        //calling inititialization

        CASE(cas);
        int Q;
        cin>>Q;

        while(Q--)
        {

            ms(visited,false);
            int r_call , c_call;
            cin>>r_call>>c_call;
            double ans = 0.00000000;
            if((r_call%2==0 && c_call%2==1) || (r_call%2==1 && c_call%2==0))
            {
                pf("%.4f\n",ans);
                continue;
            }
            int flag = 0;
            for(int ii= 0; ii<4; ii++)
            {
                int r_t = r_call + callRM[ii];
                int c_t = c_call + callCM[ii];
                if(!valid(r_t,c_t))
                    continue;
                if(graph[r_t][c_t]==2 || graph[r_t][c_t]==3 ||graph[r_t][c_t]==5 ||graph[r_t][c_t]==8)
                {
                    ans = area(r_t,c_t);
                    flag  = 1;
                    break;

                }

            }

            if(flag == 1)
                pf("%.4f\n",ans);
            else
            {
                for(int ii= 0; ii<4; ii++)
                {
                    int r_t = r_call + callRM[ii];
                    int c_t = c_call + callCM[ii];
                    if(valid(r_t,c_t))
                    {
                        ans = area(r_t,c_t);
                        pf("%.4f\n",ans);
                        break;
                    }

                }

            }





        }








    }







    return 0;
}

Thursday, March 17, 2016

All palindromic contiguous substring !

This is so far what I have come up with ! I think there are better ways to do this .  : ) )
But it 's working for my problem
:-P


Here's to the code - >

//Sorry this doesn't work ! I will edit it when I'm busy :-/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
@_@
catGotBored
@_@
*/

#include<bits/stdc++.h>
using namespace std;
int isPalindrome[100][100];
map<string,int>isThere;
void all_palindromic_conti_sub_str(string s)
{
    int N = s.length();
    //Lets find all such palindromes with length 1 . Easy , huh ?
    for(int idx = 0; idx<N; idx++)
        isPalindrome[idx][idx] = 1;

    //Lets find all such palindromes with length 2 . Easy again !!
    for(int idx = 0; idx < N -1 ; idx++)
        isPalindrome[idx][idx+1] = (s[idx] == s[idx+1] );

    //Now,lets do it for all lengths < = N

    //Idea is simple :
    //If I know s[i...j] is a palindrome then s[i-1.....j+1] is a palindrome
    //if and only if s[i-1] == s[j+1]

//O(N^2) for marking only

    for(int idx = 0 ,len = 3; idx <= N -len; idx++,len++)
    {
        for(int jdx = idx + len -1 ; jdx < N ; jdx ++ )
        {
            isPalindrome[idx][jdx] = isPalindrome[idx+1][jdx-1] & s[idx] == s[jdx] ;
        }


    }


    /

//Bad approach : O(N^3 * log (N)) -> Need to do better //
    for(int ff = 0; ff<N; ff++)
        for(int ss = 0; ss<N; ss++)
            if(isPalindrome[ff][ss])
            {
                string temp;
                for(int xfsx = ff ; xfsx <= ss  ; xfsx++)
                    temp+=s[xfsx] ;        //    cout<<s[xfsx]; //Every palindromes non distinct ones too
                if(isThere[temp] == 0)
                {
                    cout<<temp;
                    isThere[temp]++;
                    cout<<endl;
                }

            }





}

int main()
{
    string str_ = "aassaa";
    memset(isPalindrome,0,sizeof(isPalindrome)); // just for nothing 
    all_palindromic_conti_sub_str(str_);
    return  0;
}